8000 Template API · alexeyrybak/blitz Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Alexey Rybak edited this page Feb 18, 2015 · 2 revisions

[IF|UNLESS]/ELSEIF/ELSE statements

It's a simple as {{ IF $a }} if-a {{ ELSEIF $b }} else-if-b {{ ELSE }} default {{ END }}. {{ UNLESS $a }} will work instead of {{ IF $a }} as well, but no "ELSEUNLESS" is provided. Simple expressions are supported too: $a >= $b, 2 < $a, $a == "bla-bla".

The most usual example of these statements is a list which can be empty:

{{ IF $list }}
    some html code before the list
    {{ BEGIN list }} {{ $data }} {{ END list }}
    some html code after the list
{{ ELSE }}
    some html code for empty list
{{ END if-list }}

if(predicate, output_true [, output_false])

If() method is a short version for predicates. If() outputs arguments according to the first argument:

$T = new Blitz();
$T->load("{{ $num }}. {{ $name }} {{ if($rip,'[R.I.P.]') }}");

$character = array(
    array(1, 'The Dude',            0),
    array(2, 'Walter Sobchak',      0),
    array(3, 'Donny',               1), // RIP, Donny
    array(4, 'Maude Lebowski',      0),
    array(5, 'The Big Lebowski',    0),
    array(6, 'Brandt',              0),
    array(7, 'Jesus Quintana',      0),
);

foreach ($character as $i => $i_data) {
   echo $T->parse(
       array(
           'num'    => $i_data[0],
           'name'   => $i_data[1],
           'rip'    => $i_data[2]
       )
   );
}

This code outputs:

1. The Dude
2. Walter Sobchak
3. Donny [R.I.P.]
4. Maude Lebowski
5. The Big Lebowski
6. Brandt
7. Jesus Quintana

include(filename)

Include() method includes external template.

1.tpl:

{{ $what }}

php code:

$T = new Blitz();
$T->load("Where is the {{ include('1.tpl') }}, Lebowski?\n");
$T->display(array('what' => 'money'));

output:

Where's the money, Lebowski?

escape(html [, "ENT_QUOTES"|"ENT_NOQUOTES"|"ENT_COMPAT")

Escape() is a very simple html output wrapper, works like htmlspecialchars() function in PHP. There is a short alias: q(). Without second argument escape uses ENT_QUOTES as default and escapes both </> and both single and double quotes:

$T = new Blitz();
$T->load('{{ q($a); }}
{{ q($a, "ENT_COMPAT") }}
{{ q($a, "ENT_QUOTES") }}
{{ q($a, "ENT_NOQUOTES") }}
');

$T->display(array('a' => "here's a \"test\" <>\'\""));
here&#039;s a &quot;test&quot; &lt;&gt;\&#039;&quot;
here's a &quot;test&quot; &lt;&gt;\'&quot;
here&#039;s a &quot;test&quot; &lt;&gt;\&#039;&quot;
here's a "test" &lt;&gt;\'"

date(format [, arg)

Date() function formats a date. When "arg" is numerical, it is treated as UNIX timestamp integer. Otherwise it's parsed using internal PHP function php_parse_date which recognizes a lot of date formats. When "arg" is omitted - the current time is used. Format string has the same conversion specifiers as PHP function strftime.

Consider the following PHP code:

$body = <<<BODY
{{ date("%d %m %Y %H:%M:%S",\$time_num); }}
{{ date("%d %m %Y %H:%M:%S",\$time_str); }}

BODY;

$T = new Blitz();
$T->load($body);

$time_num = mktime(11, 22, 33, 7, 22, 1976);
$time_str = '1976-07-22 01:02:03';
$T->display(array(
    'time_num' => $time_num,
    'time_str' => $time_str
));

This code outputs:

22 07 1976 11:22:33
22 07 1976 01:02:03

WARNING: Before 0.7.1.10 Blitz used internal PHP timelib library by default. This binding did't work properly. If you got something like "symbol lookup error: /path/lib/php/extensions/no-debug-non-zts-20090626/blitz.so: undefined symbol: timelib_time_ctor") - then the fastest way to fix this is either to get newest source or to recompile Blitz with changed line "#define BLITZ_WITH_TIMELIB 0".

Clone this wiki locally
0