MACRO - a flexible PHP template engine with a human face

    Since today on Habr’s day PHP of template engines, I can’t help but talk about MACRO - the most flexible template engine with readable templates, among those known to me.

    The reason for its creation is simple - the WACT used by us at that time became more and more monstrous. You can read more about the reasons and the initial idea in our forum .

    The main "chips"


    Flexibility


    Inside, you can easily use regular 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 template reuse


    Powerful tools for layouting templates: wrapping ( wrap ), including ( include ), reusing ( apply ) within the same template. Examples of template magic can be found on the project wiki . Thanks to the use of MACRO, we were able to completely get rid of duplication in the templates.

    Speed


    The code of the original template is first translated into a php script ready to run, and the template is compiled in its entirety , therefore composition (splitting the template into parts, wrapping, etc.) has almost no effect on the speed of the template, since it is assembled into one class) during compilation, and the scope (context) is organized using the methods of the compiled class. That is, most of the “heavy” operations are either performed at the compilation stage, or they use the tools built into the PHP, which made it possible to achieve good “speed” indicators.

    Extensibility


    The ease of adding your tags and your filters . And also the ability to create entire packages with your own tags or filters.

    Modifiability


    Flexibility of tuning and ease of doping is achieved due to the low cohesion of the components and adequate OOP.

    Readability


    Templates, despite all the flexibility, are normally read by non-programmers, after their shallow zombies.

    Bit of tar


    MACRO is essentially syntactic sugar for native PHP. And if you don’t pay the layout designer a salary for three months, then he may well ruin the entire site, having access only to templates.

    Show me the code!


    Template example:
    {{insert into="content_zone" file="page.phtml"}}


    Автор:
    {$#photo.member.name}

    Категория:
    {$#photo.category.title}

    Название:
    {$#photo.title}

    Теги:


    {{list using='{$#photo.tags}' as='$tag'}}
     

       {{list:item}}
        
    • {$tag.title|uppercase}

    •  {{/list:item}}
       

     {{list:default}}
     Нет тегов
     {{/list:default}} 
    {{/list}}

    {{insert file="photo/marks.phtml"/}}

    {{/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 required 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, compose templates, split lists into pages and display pagers, work with forms, and so on.

    Expressions


    Expressions are used to output any variable values. The expressions in our example are {$tag.title|uppercase}and {$#photo.largeFileUrl}. The expressions in the templates are essentially the echo operation. The dot separates the parts of the path to the displayed variable. The expression is {$tag.title}equivalent .

    The expressions are described in detail in the " Expressions " section .

    Filters


    Filters are used to modify / format the values ​​displayed in expressions. Expressions with filters in our example, this {$tag.title|uppercase}. Uppercase filter - converts values ​​to uppercase. In essence, this is an alias for the strtoupper php function, which is used for the variable specified in the expression.

    Usually a filter is a wrapper for some commonly used php function. However, nothing prevents you from creating your own unique filters, since doing this is quite simple. For example, a few days ago I happened to add a filter that inclines nouns depending on the number (1 person, 2 people and more).

    Little bit about speed


    It makes no sense to develop a template engine with an eye on high speed without a set of tests. In short, at the layout close to the “combat” one, MACRO is 1.75 times slower than pure PHP, but one third faster than the nearest “human” template engine (smarty).

    Detailed test results can be viewed (and downloaded) on the corresponding page .


    Also popular now: