Since today is the day of PHP template template on Habré, I can not tell you about
MACRO - the most flexible template engine with readable templates, among those I know.
The reason for its creation is simple -
WACT , used by us at that time, became increasingly monstrous. More information about the causes and the original idea can be found
on our forum .
The main "chips"
Flexibility
Inside you can easily use the usual php-inserts, and the template engine itself contains a very small set of rules for its use. This gives us a very flexible tool, with support for both pull and push data access.
High Reuse Patterns
Powerful tools for building templates: wrapping (
wrap ), inclusion (
include ), reuse (
apply ) within the same template. Examples of patterned magic can be found
in the project wiki . Through the use of MACRO, we were able to completely get rid of duplication in the templates.
')
Speed
The initial template code is first translated into a ready-to-run php script, and the template is
compiled entirely , so the composition (splitting the template into pieces, wrapping, etc.) has almost no effect on the speed of the template, since it is assembled into one ( class) at compilation, and the scope (context) is organized using the methods of the compiled class. That is, the majority of “heavy” operations are either performed at the compilation stage, or they use tools built into PHP, which made it possible to achieve good “speed” indicators.
Extensibility
Ease of
adding your tags and
your filters . As well as the ability to create whole packages with your tags or filters.
Modifiability
The flexibility of customization and ease of finishing are achieved due to the low coupling of the components and adequate OOP.
Readability
Templates, despite all the flexibility, are normally read by non-programmers, after their shallow zombie.
Little tar
MACRO is essentially syntactic sugar for native PHP. And if you don’t pay your designer a salary for three months, then he can easily ruin the whole site, having access only to templates.
Show me the code!
Template example:
{{ insert into ="content_zone" file ="page.phtml" }}
< img src ={$# photo . largeFileUrl } />
< dl >
< dt > : </ dt >< dd > {$#photo.member.name} </ dd >
< dt > : </ dt >< dd > {$#photo.category.title} </ dd >
< dt > : </ dt >< dd > {$#photo.title} </ dd >
< dt > : </ dt >
< dd >
{{ list using='{$#photo.tags}' as='$tag' }}
< ul >
{{ list:item }}
< li > {$tag.title|uppercase} </ li >
{{ /list:item }}
</ ul >
{{ list:default }}
{{ /list:default }}
{{ /list }}
</ dd >
{{ insert file="photo/marks.phtml"/ }}
</ dl >
{{ /insert }}
* This source code was highlighted with Source Code Highlighter .
Tags
include and
list , in the example, are tags. Some tags can only be inside other tags, some tags must contain mandatory attributes, some attributes must contain only certain values, and so on. All this is checked by the compiler, and is accompanied by detailed descriptions in case of an error.
MACRO contains a sufficiently large number of tags that allow you to display lists, link patterns, split lists into pages and display pagers, work with forms, and so on.
Expressions
Output expressions are used to output any variable values. The expressions in our example are
{$tag.title|uppercase}
and
{$#photo.largeFileUrl}
. Expressions in templates are essentially echo operations. The dot separates the parts of the path to the output variable. The expression
{$tag.title}
equivalent to
<?php if(isset($tag['title'])) echo $tag['title']; ?>
<?php if(isset($tag['title'])) echo $tag['title']; ?>
.
Expressions are described in detail in the section "
Expressions ".
Filters
Filters are used to modify / format the values displayed in expressions. The filter expressions in our example are
{$tag.title|uppercase}
. Filter uppercase - converts values to uppercase. In fact, it is an alias for the php-function strtoupper, which is used for the variable specified in the expression.
Usually the filter is a wrapper for some frequently used php function. However, nothing prevents you from creating your own unique filters, since it is quite simple to do this. For example, a few days ago I had the opportunity to add a filter inclining nouns depending on the number (1 person, 2 people and further).
A bit about speed
Developing a template engine with an eye for high speed does not make sense without a set of tests. In short, on a layout close to the “combat”, the MACRO is 1.75 times slower than a pure PHP, but faster by the next “human” template maker (smarty) by a third.
Detailed test results can be viewed (and downloaded) on the
corresponding page .