Sintags
Sintags is a template language extracted from the Akelos PHP Framework. The initial goal when designing the Sintags was to allow WYSIWYG HTML editor compatibility using a simplistic approach to looping collections and printing variables.
Sintags will not prevent users from running PHP code on your views. The goal with Sintags is not building another full-fledged language. It’s to make coding views less frustrating and verbose. In fact Sintags code will be compiled into PHP for performance reasons.
The name Sintags comes uses the Spanish word “Sin” which means without. Ironically, the only tag allowed in Sintags is the <hidden></hidden> tag, which will skip the content within the tags.
In this document you’ll find the following information about Sintags:
- Basic Sintags syntax
- Ruby-esque Sintags syntax
- Download and integrate Sintags in your project
- Bugs and Contributions
Basic Sintags syntax
Basic Sintags is what most designers will need to learn to work with the views. It is composed of the following elements:
| { | 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 if element is not empty and starts a php condition block like “if(!empty($var_name)) {“. You need to close this blocks using “{end}” or <?php } ?> |
| {end} | Closes a block generating <?php } ?> |
| {object.attribute} | “.” is used for accessing object attributes. This is the same as <?php echo $object->attribute; ?> |
| {array-key} | “-” is used for accessing array on a specific key. This is the same as <?php echo $array['key']; ?> |
| _{Multilingual text} | “_{ }” will enclose a string for internationalization. |
| {_multilingual_var} | “{_ }” will enclose a variable for internationalization. This variable must be an array with the current locale as the key. |
| {\var} | “{\ }” will escape malicious html entities to avoid XSS attacks. |
| {loop people} {end} | Iterates over a collection. In this case the variable $person will be available inside the loop.. |
The following examples will show you how Sintags is converted into PHP. These have been taken from the test suite.
Printing variables, object attributes and array members
Printing variables.
Sintags
PHP
Printing object attributes.
Sintags
PHP
Printing nested object attributes.
Sintags
PHP
Printing array members.
Sintags
PHP
Printing array members with numeric indexes.
Sintags
PHP
Mixing object attributes and array members.
Sintags
PHP
Mixing array members and object attributes.
Sintags
PHP
Hiding stuff
You can remove blocks using the hidden tag. The content inside <hidden></hidden> will not be included in the compiled template.
Sintags
<?php This will not be executed ?>
</hidden>
PHP
Private variables
By convention, object attributes and array keys that start with an underscore are considered private. Therefore they will not compile to PHP.
Sintags
PHP
Filtering malicious html entities from variables
You should avid XSS attacks by escaping variables using a backslash after the opening brace.
Sintags
PHP
Also on object attributes and array elements.
Sintags
PHP
Escaping from Sintags
If you want the Sintags parser to ignore content within braces, you can escape it by using a backslash like:
Sintags
PHP
Sometimes you’ll have to print two sintags string separated by an underscore – _{} is for multilingual block as we will see later. Then you’ll have to escape the multilingual block start _ with a backslash.
Sintags
PHP
Multilingual text
By prefixing with an underscore _ a sintags block, you’ll start a multilingual block.
Sintags
PHP
You can even include HTML.
Sintags
<a href='http://google.co.uk'>Localized links</a>
</p>}
PHP
<a href=\'http://google.co.uk\'>Localized links</a>
</p>', array()); ?>
If you need to nest brackets on multilingual blocks, you can escape them using a backslash.
Sintags
PHP
If you need to include variables in multilingual Sintags blocks, you can do so by prefixing the variable with a % symbol. This will bind the variables in the second parameter of the translation method.
Sintags
PHP
You should also escape variables binded to translations when using users input.
Sintags
PHP
Escaping variables is also done by prefixing with a backslash.
Sintags
PHP
Multilingual variables
When a variable is underscored at the beginning of a Sintags block, it’s not considered private. Underscoring a variable will tell Sintags that the variable needs to be translated.
Sintags
PHP
Conditional printing
If your PHP error reporting settings are set to complain when using undeclared variables, you can prevent the notices by adding a question mark after the sintags variable.
The following example will print the variable $comment only if not empty.
Sintags
PHP
Conditional Statements
You can use {? } statement to execute some code only if a specified variable is true.
Sintags
PHP
Or the {! } statement to execute some code only if a specified variable is false.
Sintags
PHP
Conditional statements can also be used on object attributes
Sintags
{comment.author}
{end}
PHP
<?php echo $comment->author; ?>
<?php } ?>
and on any object attribute array combination
Sintags
{comment.author}
{?comment.author-name}
{comment.author-name}
{end}
{end}
PHP
<?php echo $comment->author; ?>
<?php if(!empty($comment->author['name'])) { ?>
<?php echo $comment->author['name']; ?>
<?php } ?>
<?php } ?>
You can use {else} syntax to execute a block with the opposite assertion.
Sintags
{comment.author}
{else}
Anonymous coward
{end}
PHP
<?php echo $comment->author; ?>
<?php } else { ?>
Anonymous coward
<?php } ?>
Iterating a collection of elements. Sintags loops
Perhaps one of the most common and parts of sintags iterating over an array of items. In order to do so, Sintags uses a {loop items} {end}
Sintags loops expects a plural noun for iterating and provides the singular form for each item.
Sintags
<q> {post.comment} {post.author} </q>
{end}
as you can see, the generated PHP contains some useful variables wich are helpful for formatting iterated collections
PHP
empty($posts) ? null : $post_loop_counter = 0;
empty($posts) ? null : $posts_available = count($posts);
if(!empty($posts))
foreach ($posts as $post_loop_key=>$post){
$post_loop_counter++;
$post_is_first = $post_loop_counter === 1;
$post_is_last = $post_loop_counter === $posts_available;
$post_odd_position = $post_loop_counter%2;
?>
<q> <?php echo $post->comment; ?> <?php $post->author; ?> </q>
<?php } ?>
Looping array elements will take the last item of the array as the variable name to singularize in the loop body.
Sintags
{end}
PHP
empty($items['directories']) ? null : $directory_loop_counter = 0;
empty($items['directories']) ? null : $directories_available = count($items['directories']);
if(!empty($items['directories']))
foreach ($items['directories'] as $directory_loop_key=>$directory){
$directory_loop_counter++;
$directory_is_first = $directory_loop_counter === 1;
$directory_is_last = $directory_loop_counter === $directories_available;
$directory_odd_position = $directory_loop_counter%2;
?>
<?php } ?>
The same works for object attributes. In this case given Post.author.friends it will set individual items as $friend.
Sintags
{end}
PHP
empty($Post->author->friends) ? null : $friend_loop_counter = 0;
empty($Post->author->friends) ? null : $friends_available = count($Post->author->friends);
if(!empty($Post->author->friends))
foreach ($Post->author->friends as $friend_loop_key=>$friend){
$friend_loop_counter++;
$friend_is_first = $friend_loop_counter === 1;
$friend_is_last = $friend_loop_counter === $friends_available;
$friend_odd_position = $friend_loop_counter%2;
?>
<?php } ?>
If you can’t match the convention giving a plural word, or you just want a different name in the loop, you can specify it using the the as key.
Sintags
{end}
PHP
empty($Post->versions) ? null : $Post_loop_counter = 0;
empty($Post->versions) ? null : $Posts_available = count($Post->versions);
if(!empty($Post->versions))
foreach ($Post->versions as $Post_loop_key=>$Post){
$Post_loop_counter++;
$Post_is_first = $Post_loop_counter === 1;
$Post_is_last = $Post_loop_counter === $Posts_available;
$Post_odd_position = $Post_loop_counter%2;
?>
<?php } ?>
Cleaning up lazy PHP
Sintags will expand your old-school shorthand php tags to prevent PHP from barking on runtime.
Sintags
PHP
It will also take care of preventing the famous xml declaration bug for you.
Sintags
PHP
Ruby-esque Sintags syntax
Sintags provides a shorthand method for calling helper functions in your views. This syntax is modeled after Ruby’s erb template language and makes it simpler for designers to jump from Rails to PHP views.
If you are used to Rails this might sound familiar. If you prefer writing helpers in php, go ahead, Sintags does not force you to use the Ruby-esque way.
Examples using the helpers registered by the Akelos Framework
Calling the render helper.
Sintags
PHP
Link to using object attributes.
Sintags
PHP
Linking and translating.
Sintags
PHP
More translations.
Sintags
PHP
Escaping double quotes.
Sintags
PHP
Symbols are treated as strings.
Sintags
PHP
Translating variables.
Sintags
PHP
Translating arrays.
Sintags
PHP
Using the optional parenthesis.
Sintags
PHP
The last set of options does not require to be enclosed in braces.
Sintags
PHP
Arrays without implicit indexes.
Sintags
PHP
Nesting function calls.
Sintags
PHP
Setting null’s.
Sintags
PHP
Shorthand for translate.
Sintags
PHP
Even shorter translate alias.
Sintags
PHP
Binding basic Sintags into method parameters.
Sintags
PHP
Nesting Sintags calls.
Sintags
PHP
Simpler nested call.
Sintags
PHP
Using url functions in Akelos.
Sintags
PHP
Emulating simple blocks.
Sintags
PHP
if(!empty($keys)){
foreach (array_keys((array)$keys) as $ak_sintags_key){
$key = $keys[$ak_sintags_key];
echo $key;
}
}?>
Assigning the result of a block.
Sintags
PHP
if(!empty($keys)){
$incremented = array();
foreach (array_keys((array)$keys) as $ak_sintags_key){
$key = $keys[$ak_sintags_key];
++$key;
$incremented[$ak_sintags_key] = $keys[$ak_sintags_key];
}
}?>
Simple assignments.
Sintags
PHP
Assigning array members.
Sintags
PHP
Assigning object attributes.
Sintags
PHP
Assigning the result of a helper method.
Sintags
PHP
Assigning an array.
Sintags
PHP
Assigning an array without braces.
Sintags
PHP
Downloading and installing Sintags
You can get Sintags from Github
Using Sintags
Using Sintags in your project is pretty straight forward.
include 'src/autoload.php';
// Create a Sintags instance
$Sintags = new AkSintagsParser();
$sintags_text = '{var}';
//Parse the sintags string
$php_code = $Sintags->parse($sintags_text);
$php_code will contain => <?php echo $var; ?>
You should cache generated php code to avoid hitting the Sintags engine for the same content.
Sintags helpers
You can register helpers in Sintags by defining the AK_SINTAGS_AVAILABLE_HELPERS constant with a serialized list of helpers.
'url_for' => 'url_helper',
'render' => 'controller',
'h' => 'text_helper',
)));
Using the above declaration will add the methods url_for, render and h to Ruby-esque sintags, which can be written like:
which will be converted to
The usefulness comes when things get more complex. For example nesting helpers like:
which will be converted to
Bugs and Contributions
The Project is hosted at Github so if you want to contribute, fork the project, make your changes and open a ticket to let me know what you’ve fixed or implemented so I can merge it on the main repository.
Notify about bugs at the git hub issues page
Back to projects
