loops - Simple PHP Templating With Looping -
hey wrote simple template system our designers use. uses str_replace
under hood thing.
its works great! issue i'd looping (foreach
) on data passed.
here's , example of template code
$var_c = [ [ "head" => "a", "body" => "b", "foot" => "c" ], [ "head" => "x", "body" => "y", "foot" => "z" ] ]; $tpl_vars = [ "__{var_a}__", "__{var_b}__", "__{var_c}__" ]; $real_vars = [ $var_a, $var_b, $var_c ]; str_replace($tpl_vars, $real_vars, $content_body);
note $var_c
contains array , i'd loop through array. how achieve that. structure thinking
__startloop__ loop var_c c c[head] c[body] c[foot] __endloop__
i cant seem head around how code this. :)
update: twig, smarty , likes big , cumbersome work. introduces learning curve designers adopt templating language.
see text-template class. supports conditions (if), loops (for) , filters: https://github.com/dermatthes/text-template
example template (string in variable):
hello {= name}, list of items: {for curitem in items} {=@index1}: {= curitem.name} {/for}
php-code:
<?php $data = [ "name" => "some username", "items" => [ ["name" => "first item"], ["name" => "second item"] ] ]; $tt = new texttemplate ($templatestring); echo $tt->apply ($data);
this should job.
Comments
Post a Comment