Templates are files that are used to produce HTML output. They may contain PHP code and SinTag code. In Akelos, template files end with ”.tpl”. Template files may include other template files, which are known as sub templates.
PHP is embedded in an HTML file by surrounding the PHP code with <? and ?> or <?php and ?> if you do not want output and <?= and ?> if you do want output. Consider the following loop:
Names of people <?php foreach($people as $person) : ?> Name: <?=$person->name ?><?php endforeach ?>
The Akelos Framework uses PHP as its language for templates, but some times looping and printing PHP code becomes a repetitive task that makes your view templates appear less than beautiful. Sintags is not meant to replace PHP, but to speed up development. It's good to use this syntax because it keeps views more clear. So if you start to seeing to much PHP code on your view, consider moving some view logic into the helpers.
The Akelos Framework doesn't want you force you to learn another useless template language. You have the power of PHP on your templates. But in some cases there's need for a graphic designer to create templates for views, so we have added a very limited syntax that allows you to create simple but powerful templates on WYSIWYG HTML editors.
This special syntax is known as Sintags (where “Sin” comes from the Spanish word for without) which is a set of rules on your view using only ”?”, “_”, ”{”, ”}”, “end”, ”.” and ”-” characters. Parsed Sintag is converted to PHP code and cached as a .php file in the app/views/<controller>/compiled directory for better performance.
{ Starts a Sintag block.} Ends a Sintag block.{var_name?} Asserts if given element has been set by the controller and prints the value of $var_name.{?var_name} Asserts whether an element is not empty and starts a PHP condition block like if(!empty($var_name)) {
IMPORTANT NOTE, This block must be closed! See {end}.
{end} Closes a block generating <?php } ?> {else} Inserts an 'else' generating <?php } else { ?>{object.attribute} The ”.” is used for accessing object attributes. This is the same as <?php echo $object→attribute; ?> {array-key} The ”-” is used for accessing an array on a specific key. This is the same as <?php echo $array['key']; ?> _{Multilingual text} The _{ } construct will enclose a text for internationalization. {_multilingual_var} The {_ } will enclose a variable for internationalization. This variable must actually be an array with the locale code as key.
Most of this template syntax has been added to make easy display of Active Record on the views, and handling internationalization with ease. Therefore it has the following basic constructs.
| Sintags | PHP Code | |
|---|---|---|
| Printing variables | {comment} | <?php echo $comment; ?> |
| Printing object attributes | {post.Comments} | <?php echo $post→Comments; ?> |
| {post.Comments.latest} | <?php echo $post→Comments→latest; ?> | |
| Printing array elements | {people-members}1 | <?php echo $people['members']; ?> |
| {people-0-member} | <?php echo $people[0]['member']; ?> | |
| Printing a mix of arrays and objects | {people.members-0.name} | <?php echo $people→members[0]→name; ?> |
| {posts-latest.created_at} | <?php echo $posts['latest']→created_at; ?> | |
| Create a not empty condition block | {?post-comments} {post.comment.details} {end} | See note |
| Attempt to print a variable only if has been set by the controller | {Post.comment.details?} | See note |
| Looping over arrays and Active Record collections | {loop posts} {post.comment?} {post.author?} {end}2 | See note |
| Getting a database stored multilingual value for an active record field | {_post.comment}3 | See note |
| Getting a multilingual value for a string | <h1>_{Ain't easy to translate text?}</h1> _{<p<It's really simple even to add <a href='http://google.co.uk'>Localized links</a> </p>} | See note |
| Escaping {} from your translated text | _{I need to print \{something_inside_curly_brackets\}. _\{Maybe a multilingual text example\} } | <?php echo $text_helper→translate('I need to print {something_inside_curly_brackets}. _{Maybe a multilingual text example} ', array(), $controller_name); ?> |
Create a not empty condition block:
if(!empty($post['comments'])) { echo $post->comment->details; }
Attempt to print a variable only if has been set by the controller:
echo isset($Post->comment->details) ? $Post->comment->details : '';
Looping over arrays and Active Record collections:
empty($posts) ? null : $post_loop_counter = 0; if(!empty($posts)) foreach ($posts as $post_loop_key=>$post){ $post_loop_counter++; $post_odd_position = $post_loop_counter%2; echo isset($post->comment) ? $post->comment : ''; echo isset($post->author) ? $post->author : ''; }
Getting a database stored multilingual value for an active record field
echo empty($post->comment); !is_array($post->comment)? '' : $text_helper->translate($post->comment);
Getting a multilingual value for a string. Using this function, locales are added automatically to your app/locales/CONTROLLER_NAME/ folder. Note that the multilingual text must written in the framework default language (English by default).
<h1><?php echo $text_helper->translate('Ain\'t easy to translate text?'); ?></h1> <?php echo $text_helper->translate( '<p>It\'s really simple even to add <a href=\'http://google.co.uk\'>Localized links</a> </p>'); ?>
1As you have seen, generated PHP code comes with some helpful vars automatically added that are useful when iterating collections. For instance, if we are using the $people array, Sintags generates a singular form of it when appropriate:
person_loop_counter holds current iteration pass beginning with 1, sort of like the tedious ++; on each iteration we usually codeperson_loop_key holds the current array keyperson_odd_position holds the boolean value which helps us to make nice row color changes
2We need to assign a plural “posts” on the {loop *} tag and fetch a singular “post” for retrieving single element details.
3Just by adding “_” after ”{” you will get the translated version of “comment”. In case you have English and Spanish comments you must have the following columns in your database “en_comment” and “es_comment”.
Note that $post→comment actually holds an array with the locale code as key, so you can set your own localized arrays that will not be added to the locale/ files.
For more sintag case studies you can check those sintag helper fixtures from Akelos testing.
Sub Templates allow you to include common display structures in several templates.
The common display structure may be some static code such account information:
Part of main template <?= $controller->render("shared/acctinfo") ?> Remainder of main template
Templates can share variables by using variables defined in the main template:
<?php $shared->page_title = "A Wonderful Hello" ?> <?= $controller->render("shared/header") ?>
The value “A Wonderful Hello”, is available within the shared/header sub template:
<title><?= $page_title ?></title>
Local variables may be passed from main templates to sub templates by using an array with the variable names as keys and the objects as values:
<?= $controller->render("shared/header", array('headline'=>'Welcome','person'=> $person )) ?>
The shared/header sub template can access them with:
Headline: <?= $headline ?> First name: <?= $person->first_name ?>