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

Template - part 3

Written by Davy Hellemans -

Prerequisites

What will you learn?

  • Use modifiers
  • Nested variables
  • Map your own modifiers

Tutorial

Modifiers?

What is a modifier you ask? This is a method/function you can easily apply to your variables when writing templates. A great way for designers to be able to do some funky stuff, without having to mess with your precious code. Enough said, examples plx!

<-- Store this into a file called template.tpl -->
{$myVariable}
{$myVariable|ucfirst}
{$myVariable|uppercase}
{$myVariable|substring:0:10}
{$myDate|date:'Y-m-d H:i'}
// 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');

// assign vars
$tpl->assign('myVariable', 'spoon library');
$tpl->assign('myDate', time());

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

Bazinga! This should output something like this.

spoon library
Spoon library
SPOON LIBRARY
spoon libr
2010-02-06 21:45

I assume most of this speaks for itself. The modifier 'ucfirst' is applied to the string contained in '$myVariable'. It is given as the first argument to the function or method that is mapped to the keyword 'ucfirst'. In case of 'substring', two extra arguments are given.

Arguments are always separated by colons. If your argument is an integer, you don't need to encapsulate with single quotes. If it's a string or any other type of variable, you have to use single quotes. The modifier date applied in the example uses this approach.

It's not a problem if you want to use colons in your arguments because of the single quotes!

Nested variables

This is the cool part. Variables can easily be nested by using them inside modifier arguments. Doesn't make any sense? Let's see the previous example but spice it up a bit by chaining the modifiers and using some variables as arguments.

<-- Store this into a file called template.tpl -->
{$myVariable}
{$myVariable|lowercase|ucfirst}
{$myVariable|uppercase|shuffle}
{$myVariable|substring:{$start}:{$length}}
{$myDate|date:{$format}}
// 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');

// assign vars
$tpl->assign('myVariable', 'spoon library');
$tpl->assign('myDate', time());
$tpl->assign('start', 0);
$tpl->assign('length', 7);
$tpl->assign('format', 'Y-m-d @ H:i');

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

Wondering what kinky stuff the dog dragged in?

spoon library
Spoon library
OOA RLISBPYRN
spoon l
2010-02-06 @ 22:02

First things first. Modifiers are, as you can see, executed from left to right. The first argument passed to the modifier is always the contents of the variable (or the already modified one, considering you chained them together).

Arguments don't necessarily need to be strings, they can be variables as well. You can even add modifiers to those variables if you want to. If you provide a variable as an argument, you shouldn't use single quotes. It gets even cooler. If you use variables as an argument, you can use characters that would not be allowed if typed manually in the template file. For example the @ sign.

Mapping custom modifiers

I assume you're starting to see the bigger picture. Being able to manipulate your view from the template without having to touch your code. But it gets even better. You can even clear the default list of mapped attributes and add some of your own.

Say you want to use sprintf or Spoon::dump.

<-- Store this into a file called template.tpl -->
{$comments|sprintf:{$numComments}}
// 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');

// clear the list of modifiers
SpoonTemplateModifiers::clearModifiers();

/*
 * Map the function sprintf to the modifier 'sprintf'. I you want to
 * map a static method you need to privode the function as an array
 * eg array('Class', 'Method');
 */
$tpl->mapModifier('sprintf', 'sprintf');

// assign vars
$tpl->assign('comments', 'There are now %s comments');
$tpl->assign('numComments', 137);

// show the output, using 'template.tpl'
$tpl->display('template.tpl')
There are now 137 comments

Self explanatory right? Oh, and close your mouth plx!

List of default modifiers

For the sake of being complete, I've added the list of default modifiers.

Conclusion

With great power comes great responsibility. You can go crazy with these modifiers, but I don't think you want to end up doing queries in your templates just because you can. Go now and play with your new toys!

3 comments

Lun wrote 2 years ago

$a = array();
$b = array();
foreach ($a as $c){
foreach ($b as $d){
if($c ==$d){
$e[$i] = $d
}
}
}

like this in tempale ,how do it?

Bauffman wrote 2 years ago

@lun
What you want to do is currently not possible. At this time you can't use if/else clausules within the template engine.

router 3g wrote 1 year ago

Someone essentially help to make significantly posts I might state.
That is the very first time I frequented your website page and thus
far? I surprised with the analysis you made
to make this actual post amazing. Fantastic job!