From the
Blitz documentation: Extremely fast and powerful template engine for very large Internet projects.
I will cite a few facts:
- This is the template engine used by Habrom;
- This template engine is used on high-load projects, it is written in C, connects as a PHP extension;
- Its speed is comparable to php itself (benchmark under the cut);
- The designers will be happy, because the templates have no application logic, no cycles, branching, etc .;
- One of its authors is Alexei Rybak fisher .
')

Blitz is a
block template engine that fundamentally distinguishes it from Smarty and the like.
Smarty compared with Blitz - a programming language.
Blitz template syntax is based on 3 constructions:
- blocks (they are contexts): {{BEGIN blockName}} block contents {{END}}
- variables: {{$ var}}
- function calls {{myFunc ($ params)}}
Loops, conditional statements, and the other “rich” syntax are missing. This ensures the separation of logic.
applications from presentation logic. As a result, the templates with the growth of the project does not turn into a mess.
From the point of view of code organization, the View component (MVC) can be divided into two parts:
- Template (HTML file with Blitz tags);
- The template controller is a 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 that programmers do much more than
more effective than layout designers.
A few examples:
1. By tradition: Hello, world!
Template.tpl template:
Hello, {{$ name}}!
script:
$ template = new Blitz ('template.tpl');
echo $ template-> parse (array ('name' => 'world'));
2. The template can be downloaded not only from the file:
To experiment with Blitz, you can download
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 ();
Result
"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 operator END:
{{END block}}.
4. Block output several times (lists or cycles):
Each block can be "iterated" several times to display lists (similar to cycles in Smarty), the 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 we will try to display the same list, but separated by commas.
This can be done in two ways: use a block or an if statement.
To begin, 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 does not look very elegant. In such simple cases it is more appropriate to use an if statement:
hello {{BEGIN block}} {{if ($ _ first, '', ',')}}} {{$ name}} {{END}}.
Blitz has predefined variables for blocks:
$ _first (first iteration),
$ _last (last iteration),
$ _total (total count),
$ _num ,
$ _even ,
$ _odd .
The name is easy to guess about the appointment. You can also define
their 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 — full or relative.
the path, all further setes and relative paths will use this default path:
$ template-> context ('/ block');
foreach (array ('Dude', 'Sobchak', 'Donny') as $ i_name) {
$ template-> iterate ();
$ template-> set (array ('name' => $ i_name));
}
The
context method works as a cd console command;
The
iterate method "outputs" a block.
Using a
block is easier, but in many cases you will need
low-level operations with context and iteration.
7. Everything is an iteration
Each template state 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 methods
block ,
set ,
iterate ,
context -
Blitz modifies this array. When the
parse method is called - blitz renders
pattern based on this structure.
Practice different calls and see what is in the dump for quick debugging.
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);
some.tpl:
{{BEGIN block}} {{if ($ _ first, '', ',')}} {{$ name}} {{END}}
The same result as in the previous examples, but
without using 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 ();
performance result:
soldiers come nowhere blinded by their faith
If you understand how it works - you are fully aware
contexts and iterations.
10. Can work with parts of the template
Sometimes it is convenient to take part of the template (block), regardless of the entire template:
This can be done using the
fetch method:
some.tpl:
{{BEGIN hello}} hello, {{$ name}} {{END}}
{{BEGIN bye}} bye, {{$ name}} {{END}}
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); }}
means
That you cause method
my_test with parameters.
We add the function
my_test 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 ();
Result:
user method call test: user method called (blitztemplate, 5), a = test
Finally:
This introduction was written as a free translation of the tutorial:
Quck Geek Blitz Tutorial .
It only illustrates the basics of using this template engine, for details
It is worth referring
to the documentation .
Links
Blitz project website:
alexeyrybak.com/blitz/blitz_ru.htmlExamples of patterns and benchmark:
alexeyrybak.com/blitz/lebowski_bench.tar.gz