Reputation: 65
Smarty version 2 if I capture a static string :
{capture name=test}
a huge page or string can go here
{/capture}
I can simply use {$smarty.capture.test} to dump my captured string in both my current template or in a child template no problem
If I try capture a "{foreach}" loop like:
{capture name=test}
{foreach item=Row from=$DataGrid.Rows name=RowsGrid}
,['{$Row.DataCells.project_name.Data}', {$Row.DataCells.approved_budget.Data}]
{/foreach}
{/capture}
I can use it easily in the current template like:
{$smarty.capture.test}
and it displays the correct data as a string. however when I try use it in a child template :
{include file='/full/path/child.tpl'}
{$smarty.capture.test}
it will result in empty captured data like:
,['', ] ,['', ] ,['', ]
if I use {$smarty.capture.test|var_dump} it shows as "string(86)" what am I missing here?
Upvotes: 1
Views: 2022
Reputation: 111839
It works for me without a problem:
PHP file:
$smarty->assign('data', array(
array('name' => 'a1'),
array('name' => 'a2'),
array('name' => 'a3'),
array('name' => 'a4'),
array('name' => 'a5')
));
$smarty->display('normal.tpl');
normal.tpl
file:
{include file='child.tpl'}
{$smarty.capture.test}
child.tpl
file:
{capture name=test}
{foreach item=Row from=$data}
,['{$Row.name}']
{/foreach}
{/capture}
Output for this is as expected:
,['a1'] ,['a2'] ,['a3'] ,['a4'] ,['a5']
You should make sure that you have your variables assigned correctly and if it's not the case you should provide more details.
Upvotes: 0
Reputation: 41958
I am assuming in your last sample that child.tpl
fills the test variable, and you then try to use it in the including template afterwards. Given how Smarty handles scope the empty output is completely right - scope inherits only downwards. A parent cannot access variables set by its child.
Apparently using the variable directly in a filter bypasses the scope checks, which would certainly be a bug in the implementation. Then again, Smarty 2 is years old, v3 has been with us for years and Twig is far more practical in the HTML5 age.
Upvotes: 0