简体   繁体   中英

Repeating a template in smarty ( no logic in template)

I dont want to write my loop in my tpl file.

I want to do something like this

foreach($arr as $val=>$key){
   $tpl->push($val); //not assign
}
//
$tpl->repeatDisplay('my.tpl');

or something like this

$tpl->repeatFor("mytile",$arr);
$tpl->repeatDisplay('my.tpl');

I dont thias there is any other way around this, so try

foreach($arr as $val=>$key){
   $tpl->assign('var',$val);
   $tpl->display('my.tpl'); 
}

What you can do is simple using fetch instead of display :

$output = '';
foreach($arr as $val=>$key){
   $tpl->assign('var',$val);
   $output .= $tpl->fetch('my.tpl'); 
}

echo $output;

But if you want to use it only to display simple variable in my.tpl it doesn't make much sense as you can read in comments. Using loops in templates is simple fine is you use it for displaying data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM