Older Newer
Thu, 09 Mar 2006 21:25:24 . . . . (vsviridov)?

Changes by last author:

Imagine you want to output all your tiny little block wrapped up in a nice table with rounded corners or something similar.

There are various approaches. It could be a smarty template with a $content variable, in said table. This way you can do following:













PHP File



$smarty->assign('content', $smarty->fetch('block.html'));

$smarty->assign('block_placeholder', $smarty->fetch('wrap_table.html'));




This method works, but you have to repeat it for every block that you might have.

Here's an alternative solution: (most of it is taken from the standard {eval}...{/eval} block function.)


function smarty_block_wrap($params, $content, &$smarty)


$smarty = clone($smarty); //Copy the original class, so there's no garbage variables after we finish



if(!isset($params['template']) || !isset($params['insert']))


return "Error, template or insert are not set";


$template = $params['template'];

$insert = $params['insert'];

unset($params['insert'], $params['template']);

//Compile content to ensure all smarty tags get processed

$smarty->_compile_source('wrap_evaluated_template', $content, $_var_compiled);


$smarty->_eval('?>' . $_var_compiled);

$_contents = ob_get_contents();


$smarty->assign($insert, $_contents);

foreach($params as $k=>$v)


$smarty->assign($k, $v);


return $smarty->fetch($template);



So now in the page you can simply do this


{wrap template="wrap_table" insert="content" other="variable" another="stuff" variables=$work_too|default:"modifiers as well"}

block content here {if $var}smarty constructs work inside this block, yay !{/if}