Introduction to the Blitz Template Engine

Original author: Alexey Rybak
  • Transfer
From the Blitz documentation : Extremely fast and powerful template engine for very large Internet projects.

I will cite a few facts:
  1. This is the template engine used by Habré;
  2. This template engine is used on highly loaded projects, it is written in C, it is connected as a PHP extension;
  3. Its speed is comparable to php itself (benchmark under the cut);
  4. Layout designers will be happy, because the templates do not have application logic, there are no loops, branches, etc .;
  5. One of its authors is Aleksey Rybak fisher .

Benchmark templating

Blitz is a block template engine that makes it fundamentally different from Smarty and the like.
Smarty compared to Blitz is a programming language.

Blitz template syntax is based on 3 constructs:
  1. blocks (they are contexts): {{BEGIN blockName}} block contents {{END}}
  2. variables: {{$ var}}
  3. function calls {{myFunc ($ params)}}

Loops, conditional statements, and other rich syntax are missing. This ensures separation of
application logic from presentation logic. As a result, templates do not turn into a mess with the growth of the project.

From the point of view of code organization, the View (MVC) component can be divided into two parts:
  1. template (HTML file with Blitz tags);
  2. template controller - this is the Blitz object that controls the processing of the template (not to be confused with the web application controller).

The template controller carries all the redundant logic for the template with which programmers deal much more
efficiently than layout designers.

A few examples:

1. By tradition: Hello, world!

Template template.tpl:
    Hello, {{$ name}}!

    $ template = new Blitz ('template.tpl');
    echo $ template-> parse (array ('name' => 'world'));

2. The template can be downloaded not only from the file:

For experiments with Blitz, you can load the
template directly in the script code from a variable:
    $ template = new Blitz ();
    $ template-> load ('Hello, {{$ name}}!');
    echo $ template-> parse (array ('name' => 'world'));

3. Blocks (contexts):

A block is part of a template that can be displayed if necessary:
    hello {{BEGIN block}} {{$ name}} {{END}}

By default, this template displays the string "hello", the block will be hidden.

The following code will print the block once:
    $ template = new Blitz ('some.tpl');
    $ template-> block ('/ block', array ('name' => 'Dude'));
    echo $ template-> parse ();

"hello dude".

Block's synonym is context. The output of the block is called iteration.

For clarity, you can specify the name of the block after the END statement:
{{END block}}.
4. Block output several times (lists or loops):

Each block can be “iterated” several times to display lists (analogue of cycles in Smarty), template controller code:
    foreach (array ('Dude', 'Sobchak', 'Donny') as $ i_name) {
      $ template-> block ('/ block', array ('name' => $ i_name);

Having executed this code for the template from the previous example, we will see:
    "Hello Dude Donny Sobchak"

5. Terms

Now let's try to display the same list, but separated by commas.
There are two ways to do this: use a block or an if statement.

First, use the block:
  hello {{BEGIN block}} {{BEGIN comma}}, {{END}} {{$ name}} {{END}}

and iterate it in a loop:
    $ need_comma = FALSE;
    foreach (array ('Dude', 'Sobchak', 'Donny') as $ i_name) {
       if ($ need_comma) {
           $ template-> block ('/ block / comma');
       } else {
           $ need_comma = TRUE;
       $ template-> block ('/ block', array ('name' => $ i_name);

For me, it doesn’t look so elegant. In such simple cases, it is more appropriate to use the if statement:

    hello {{BEGIN block}} {{if ($ _ first, '', ',')}} {{$ name}} {{END}}.

Blitz has predefined variables for blocks: $ _first (first iteration),
$ _last (last iteration), $ _total (total number), $ _num , $ _even , $ _odd .
The name is easy to guess about the purpose. You can also define
your variables from the template controller.

A couple more if examples:
    {{if (TRUE, '2 + 2 = 4', '2 + 2 = 5'); }}
    {{if ($ a, "b", $ c); }}

6. Using contexts and iterations

Instead of a block, you can use a combination of three low-level methods:
context , set, and iterate . The context method takes one parameter - the full or relative
path, all further sets and relative paths will use this path by default:
    $ template-> context ('/ block');
    foreach (array ('Dude', 'Sobchak', 'Donny') as $ i_name) {
      $ template-> iterate ();
      $ template-> set (array ('name' => $ i_name));

The context method works like a cd console command;
The iterate method "displays" the block.

Using block is simpler, but in many cases you will need
low-level operations with context and iteration.

7. Everything is an iteration

Each state of the template can be described by a specific data structure.
If you add the following code to the previous example:
    $ data = $ template-> getIterations ();

$ data will have the following structure:
  array (
      0 => array (
          'block' => array (
               0 => array ('name' => 'Dude'),
               1 => array ('name' => 'Sobchak'),
               2 => array ('name' => 'Donny')

This is the internal state of the template before calling the parse method .

Whatever you do with the template using the block , set , iterate , context methods -
Blitz modifies this array. When the parse method is called , blitz renders a
template based on this structure.

Practice different challenges and see what happens in the dump to quickly debug
your code.

8. All the same, through the array:

    $ data = array (
        0 => array (
            'block' => array (
                 0 => array ('name' => 'Dude'),
                 1 => array ('name' => 'Sobchak'),
                 2 => array ('name' => 'Donny')
    $ template = new Blitz ('some.tpl');
    echo $ template-> parse ($ data);

    {{BEGIN block}} {{if ($ _ first, '', ',')}} {{$ name}} {{END}}

The same result as in the previous examples, but
without using the block / context / iterate methods.

9. Nested iterations

    $ data = array (
        array (
            'who' => 'soldiers',
            'what' => array (
                0 => array (
                    'verb' => 'going',
                    'details' => array (
                        0 => array ('item' => 'nowhere'),
                1 => array (
                    'verb' => 'blinded',
                    'details' => array (
                        0 => array ('item' => 'by'),
                        1 => array ('item' => 'their'),
                        2 => array ('item' => 'faith')
    $ template = new Blitz ();
    $ template-> load ('{{$ who}} {{BEGIN what}} {{$ verb}} {{BEGIN details}} {{$ item}} {{END}} {{END}}');
    $ template-> set ($ data);
    echo $ template-> parse ();

execution result:
    soldiers going nowhere blinded by their faith

If you understand how this works, you are fully aware of the
contexts and iterations.

10. You can work with parts of the template

Sometimes it’s convenient to take part of a template (block) regardless of the whole template:
This can be done using the fetch :

some.tpl method:
    {{BEGIN hello}} hello, {{$ name}} {{END}}
    {{BEGIN bye}} bye, {{$ name}} {{END}}

the code:
    echo $ template-> fetch ('/ hello', array ('name' => 'Lena')); // hello, Lena
    echo $ template-> fetch ('/ bye', array ('name' => 'Sveta')); // bye, Sveta

11. Function calls in the template:

Following code
{{my_test ($ a, "foo", 'bar', TRUE, 2005); }}
that you are calling the my_test method with parameters. We add the my_test

function as follows:
  class View extends Blitz {
      function my_test ($ a) {
          return 'user method called (' .__ CLASS __. ',' .__ LINE__. '), a ='. $ a;
  $ template = new View ();
  $ template-> load ('user method call test: {{my_test ("test")}}');
  echo $ template-> parse ();

    user method call test: user method called (blitztemplate, 5), a = test


This introduction was written as a free translation of a tutorial: Quck Geek Blitz Tutorial .
It only illustrates the basics of using this template engine; for details
, refer to the documentation .


Blitz project website:
Examples of templates and benchmark:

Also popular now: