Templates within templates

You can feed the output of a ViewFish template into another template


The band Yes

ABOUT THE BAND YES

The band Yes formed in 1968. They've released 21 studio albums. Some members of the band are:

<?php

# require the templating library 
require '../source/ViewFish.php'; 

# first, you instantiate a ViewFish object
$T = new ViewFish\viewfish();

# 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'],
];

# load demo2 template
$template2    = $T->load_template("demo2-1.tmpl");
$bandlist     = null;
foreach($members as $member) {
    # render the template and appoend it to the $bandlist variable
    $bandlist .= $T->render($template2,$member); 
}

# we don't need to instantiate the object ($T) again to use another template, just load a new template
$template3 = $T->load_template("demo3.tmpl");

# we can use the output of the template above (stored in $bandlist) to feed into another template
$band = [
    'bandname' => 'yes',
    'year_formed'=>1968,
    'number_albums'=>21.2, //this will be corrected via intval
    'bandlist'=>$bandlist
];
echo $T->render($template3,$band); 
<li>{{firstname|ucwords}} {{lastname|ucwords}} plays {{instrument}}.</li>
<html>
    <head>
        <title>The band {{bandname|ucwords}}</title>
    </head>
    <body>
        <h1>ABOUT THE BAND {{bandname|upper}}</h1>
        <p>The band {{bandname|ucfirst}} formed in {{year_formed}}.  They've released {{number_albums|intval}} studio albums. Some members of the band are: </p>
        <ul>
            {{bandlist}}
        </ul>
    </body>
</html>