Prerequisites
What will you learn?
- Load and display templates
- Assign variables
- Simplified if/else control structures
Tutorial
Template basics
Templates provide a great way to separate code from layout. You can compare it to inline style attrbutes in HTML versus CSS files. Another great advantage is that designers don't need to know PHP to be able to modify or create templates.
SpoonTemplate applies a common used technique to compile its' templates to PHP code. This is done to speed up the actual displaying of templates.
Assigning variables
The 3 most important things in the template are: assigning variables, optional blocks and iterating arrays. In this tutorial we'll only be going over the first 2.
In the example below, 2 values are assigned to a template. You can then use those 2 variables in the template code. There's no limit on the number of times you can use a template variable.
// required classes
require_once 'spoon/spoon.php';
/* contents of our template.tpl file:
My name is {$name} and I live in {$city}.
*/
// load template
$tpl = new SpoonTemplate();
/*
* Should this template be recompiled to PHP every time you execute this
* PHP script. This is encouraged to be enabled during development, since
* changes to your template file won't be visible otherwise.
*/
$tpl->setForceCompile(true);
/*
* By default, the compiled templates will be stored in the directory
* wherein your PHP script resides. Since this is nasty, we advise you
* to choose a good location where these files may be stored.
*/
$tpl->setCompileDirectory('/home/my_project/compiled_templates');
// assign some variables
$tpl->assign('name', 'Davy Hellemans');
$tpl->assign('city', 'Gent');
// show the output, using 'template.tpl'
$tpl->display('template.tpl')
This example gives the following output.
My name is Davy Hellemans and I live in Gent.
Optional elements
Options are optional elements, based on the assigned variables. Each variable is also available as an option in your template. If the value of this variable is not empty or false, it's considered true and allows for simple if/else structures. Inverse options are also available, considering the value of your variable results to false. The syntax of both these options is very similar, except for the exclamation mark. Just as with variables, there's no limit on the amount of times you can use an option. Also note that options may even be nested.
// required classes
require_once 'spoon/spoon.php';
/* contents of our template.tpl file:
Name: {$name}
City: {$city}
{option:email}E-mail: {$email}{/option:email}
{option:!email}Woops, we don't know your e-mail{/option:!email}
*/
// load template
$tpl = new SpoonTemplate();
// force compiling
$tpl->setForceCompile(true);
// choose compile directory
$tpl->setCompileDirectory('/home/my_project/compiled_templates');
// assign some variables
$tpl->assign('name', 'Davy Hellemans');
$tpl->assign('city', 'Gent');
$tpl->assign('email', 'davy@spoon-library.be');
// show the output, using 'template.tpl'
$tpl->display('template.tpl')
This example gives the following output.
Name: Davy Hellemans
City: Gent
E-mail: davy@spoon-library.be
If we wouldn't have assigned a value for 'email', then the inverse option would be shown, providing the following output:
Name: Davy Hellemans
City: Gent
Woops, we don't know your e-mail
Conclusion
You can now assign values to variables in an easy way. Of course there's more, but this is just an introduction. The upcoming tutorial concerning SpoonTemplate will show you how to iterate over variables in templates.
Arnout wrote 2 years ago
{if $alpha == 'Test'}
<p>Test it, baby!</p>
{elseif $beta == 'No test'}
<p>Sorry, no testing!</p>
{else}
<p>Damn, what's this!</p>
{/if}
Thx!