Note: each tutorial contains information about one specific part of Spoon. If something is unclear, post a comment.

Template - part 2

Written by Davy Hellemans -

Prerequisites

What will you learn?

  • Iterate variables
  • Use cycle tags

Tutorial

Basic iteration

Iterations are used to iterate variables, more specifically 'arrays'. You will surely be using a lot of these loops when building an application with Spoon.

The first example is the most basic form of an iteration including a single variable.

<-- Store this into a file called template.tpl -->
{iteration:authors}
  {$authors.name}
{/iteration:authors}

As you can see this is pretty straightforward. Using the name of the iteration before the dot in a variable tells the template engine that you want to use the 'name' variable from the keys of the 'authors' array.

// required classes
require_once 'spoon/spoon.php';

// load template
$tpl = new SpoonTemplate();

// force compiling
$tpl->setForceCompile(true);

// choose compile directory
$tpl->setCompileDirectory('/home/my_project/compiled_templates');

// build list of authors
$authors = array();
$authors[] = array('name' => 'Davy Hellemans');
$authors[] = array('name' => 'Tijs Verkoyen');
$authors[] = array('name' => 'Dave Lens');

// assign list
$tpl->assign('authors', $authors);

// show the output, using 'template.tpl'
$tpl->display('template.tpl')

This example gives the following output.

Davy Hellemans
Tijs Verkoyen
Dave Lens

Cycle tag

Sometimes you no need to alternate row colors in a table with odd/even classes. Or if you just want to put a <br /> after the 2nd element in a row. You can achieve this by using the 'cycle' tag in an iteration.

<-- Store this into a file called template.tpl -->
{iteration:letters}
  {$letters.character} {cycle:'- ':'<br />'}
{/iteration:letters}
// required classes
require_once 'spoon/spoon.php';

// load template
$tpl = new SpoonTemplate();

// force compiling
$tpl->setForceCompile(true);

// choose compile directory
$tpl->setCompileDirectory('/home/my_project/compiled_templates');

// build list of letters
$letters = array();
$letters[] = array('character' => 'a');
$letters[] = array('character' => 'b');
$letters[] = array('character' => 'c');
$letters[] = array('character' => 'd');
$letters[] = array('character' => 'e');

// assign list
$tpl->assign('letters', $letters);

// show the output, using 'template.tpl'
$tpl->display('template.tpl')

This example gives the following output.

a - b
c - d
e -

I believe the output speaks for itself. Every second element in the iteration receives an additional linebreak. This makes sure that we only have two items on each line.

You could also display the letters separated with a dash except for the last one by using the integrated first/last options. Change your template code to the example below.

<-- Store this into a file called template.tpl -->
{iteration:letters}
  {$letters.character} {option:!letters.last}- {/option:!letters.last}
{/iteration:letters}

When you execute the same code, using the template above, you get the following output.

a-b-c-d-e

Nested iterations

Sure thing! Iterations can be nested pretty easy. All you need to do is make sure the array format you're using is composed in a correct manner. The example below demonstrates an iteration 2 levels deep.

<-- Store this into a file called template.tpl -->
<ul>
   {iteration:authors}
      <li>
         {$authors.name}
         <ul>
            {iteration:authors.websites}
               <li><a href="{$authors.websites.url}">{$authors.websites.url}</a></li>
            {/iteration:authors.websites}
         </ul>
      </li>
   {/iteration:authors}
</ul>
// required classes
require_once 'spoon/spoon.php';

// load template
$tpl = new SpoonTemplate();

// force compiling
$tpl->setForceCompile(true);

// choose compile directory
$tpl->setCompileDirectory('/home/my_project/compiled_templates');

// build list authors
$authors = array();
$authors[0]['name'] = 'Davy Hellemans';
$authors[0]['websites'][] = array('url' => 'http://www.spoon-library.be');
$authors[0]['websites'][] = array('url' => 'http://blog.bauffman.be');
$authors[1]['name'] = 'Tijs Verkoyen';
$authors[1]['websites'][] = array('url' => 'http://blog.verkoyen.eu');
$authors[1]['websites'][] = array('url' => 'http://classes.verkoyen.eu');
$authors[2]['name'] = 'Dave Lens';
$authors[2]['websites'][] = array('url' => 'http://www.adeepersilence.be');

// assign list
$tpl->assign('authors', $authors);

// show the output, using 'template.tpl'
$tpl->display('template.tpl')

You should get a list just like the one displayed below.

Davy Hellemans
* http://www.spoon-library.be
* http://blog.bauffman.be

Tijs Verkoyen
* http://blog.verkoyen.eu
* http://classes.verkoyen.eu

Dave Lens
* http://www.adeepersilence.be

Conclusion

Add 'ability to loop variables' to your arsenal of template trickery. Mastering iterations is crucial when using the template engine. In the next chapter we'll be explaining template modifiers.

1 comment

Lorenz Quintens wrote 1 year ago

Is it possible to iterate over associative arrays, so that both key and value are available as variables?