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


Changes by last author:

Added:
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:

Template

<code>

<table>

...

<tr>

<td>

{$content}

</td>

</tr>

...

</table>

</code>

PHP File

<code>

<?php

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

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

$smarty->display('page.html');

?>

</code>

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.)

<code>

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($content))

return;

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);

ob_start();

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

$_contents = ob_get_contents();

ob_end_clean();

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

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

{

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

}

return $smarty->fetch($template);

}

</code>

So now in the page you can simply do this

<code>

{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}

{/wrap}

</code>