-
Notifications
You must be signed in to change notification settings - Fork 12
b8\View\Template
b8\View\Template is an extension of b8\View that implements a simple, but fully functional template language.
There are two ways to load a Template object, depending on whether or not your templates are stored in files. These methods are as follows:
$template = b8\View\Template::createFromFile('group/template');
Using this method, b8 will look in your configured view path for a file named 'template' in directory 'group'. The file should have the extension .html.
$template = b8\View\Template::createFromString('{for myarray.items}{@value.name}{/for}');
Using this method, b8 will create a template using the provided string as the template content. This could, for example, be loaded from a database or a remote API.
Passing variables to your templates, and using them within your templates, is simple. You simply set the variables as properties on the template class, and they will immediately be available within the template. Like so:
PHP:
$template = Template::createFromFile('news/list');
$template->newsItems = $newsStore->getByCategory(News::CATEGORY_TECH);
Template:
<h1>Tech News</h1>
{for newsItems.items}
<h2><a href="/article/{@value.id}">{@value.title}</a></h2>
<p>{@value.summary}</p>
{/for}
Once you've loaded your template, and passed in all of the variables you want to work with, you simply render the template to return it's output:
$template = Template::createFromFile('news/list');
$template->newsItems = $newsStore->getByCategory(News::CATEGORY_TECH);
print $template->render();
b8 provides some built in template functionality, as well as the ability to expand it with functions of your own. The built in constructs are as follows:
Variables can be included into any template using the @ syntax demonstrated above, like so:
Hello {@whom}
Objects and arrays can be accessed using a simple dot-based syntax:
Hello, my name is {@animal.name} - I am a {@animal.species}.
As this works with both objects and arrays, this could represent $animal['name'] or $animal->name as appropriate. These can be mixed (with arrays in objects and vice-versa.)
Boolean If
{if myObject.isModified}
Object is modified.
Boolean If Not
{ifnot myObject.isModified}
Object remains unchanged.
{/ifnot}
If
{if myObject.id = 1}
You have object ID 1.
{/if}
Else
{if myObject.id = 1}
You have object ID 1.
{else}
You do not have object ID 1.
{/if}
### Loops Instead of providing three different types of loops (for, foreach and while) - b8 provides one type of loop that can be used multiple ways, the for loop:
For Each:
Foreach takes an array as an argument, and loops through it. For each iteration, it puts into your template scope a key and a value variable.
{for news.items}
{@key} => {@value}
{/for}
For Range
The "traditional" for loop:
{for 0:100; i++}
I have said hello {@i} times.
{/for}
With this type of loop, you can also use variables for either side of the range argument:
{for 0:pages.count; page++}
<a href="/page/{@page}">{@page}</a>
{/for}
There is a simple syntax for making template function calls:
{getNews category: 1; count: 10}
To break down the above, getNews is your function name and everything following that are arguments which will be passed to the handling function as an associative array.
Including templates in your templates:
This is a built-in function, provided to allow you to embed templates within your templates.
{include template: news/list; variables: newsItems,categoryTitle => title}
In the example above, we're including the "news/list" template, passing into the new scope the newsItems and categoryTitle variables from our current scope. In addition, when we pass "categoryTitle" to the sub-template, it will be given the value as "title" instead. This allows you to pass the appropriate variable names to suit your templates, rather than relying on always having the correct variable available.
The variables argument in the above example can also be omitted.