Template Reuse

You can use a template multiple times.


Here's one way to loop, in PHP
A second way is in the template itself
<?php

# require the templating library 
require '../source/smallaxe-templating.php'

# first, you instantiate a Small Axe object
$T = new Smallaxe\smallaxe_template();

# set the template path
$T->set_template_path(__DIR__.'/../templates/');

$members = [
    [
'firstname'=>'steve''lastname'=>'howe''instrument'=>'guitar'],
    [
'firstname'=>'jon''lastname'=>'anderson''instrument'=>'vocals'],
    [
'firstname'=>'rick''lastname'=>'wakeman''instrument'=>'keyboards'],
    [
'firstname'=>'bill''lastname'=>'bruford''instrument'=>'drums'],
    [
'firstname'=>'chris''lastname'=>'squire''instrument'=>'bass'],
];

# here's one way to loop through, in PHP
$template2     $T->load_template("demo2-1.tmpl");
echo 
"Here's one way to loop, in PHP<ul>";
$bandlist '';
foreach(
$members as $member) {
    
$bandlist .= $T->render($template2,$member); 
}
echo 
$bandlist
echo 
"</ul>\n\n";

echo 
"<hr>";

# here's another way, in a template
echo "A second way is in the template itself";
$template2     $T->load_template("demo2-2.tmpl");
$args['members'] = $members
echo 
$T->render($template2,$args); 
<li>{{firstname|ucwords}} {{lastname|ucwords}} plays {{instrument}}.</li>
<ul>
{{@loop data=members}}
<li>{{firstname|ucwords}} {{lastname|ucwords}} plays {{instrument}}.</li>
{{/loop}}
</ul>