-
Notifications
You must be signed in to change notification settings - Fork 42
Callbacks and plugins
Maksim Kochkin edited this page Feb 18, 2015
·
1 revision
Blitz supports callbacks like {{ Some::doSomething($params) }} or {{ doSomething($param) }}. With this you can add any function calls to your template.
Basically you can:
- extend Blitz and use your custom template methods in the template code
- call any other PHP function like do($something) or Plugin::do($something)
Callbacks were added in 0.7.1.5 but changed in older versions. Current callback API (writing this for 0.7.1.14) works this way:
-
{{ this::method }}calls class method through the template object -
{{ php::method }}calls PHP function "method" -
{{ helper::method }}calls static PHP-method -
{{ method }}calls class method first and PHP-method after (if not found) and vice versa - according toblitz.php_callbacks_firstsetting. This was added in 0.7.1.14 and by default it's 1 so PHP-method is called first. This can be changed in next versions according to the community reaction.
See the following example:
// calls.php
<?
ini_set("blitz.php_callbacks_first", 1);
$body = <<<BODY
{{ php::date("Y/m/d H:i") }}
self-call: {{ this::doSomething() }}
php-call: {{ php::doSomething() }}
this+php call: {{ doSomething() }}
this+php call: {{ doSomething2() }}
BODY;
function doSomething() {
return 'PHP :: did comething';
}
class View extends Blitz {
function doSomething() {
return 'THIS :: did something';
}
function __call($a, $b) {
return '__call handler: '.var_export($a, true).', '.
str_replace(array("\n","\r"), "", var_export($b, true));
}
}
$T = new View();
$T->load($body);
$T->display();
?>fisher@fisher:~/prj/blitz/tests> php5 calls.php
2010/04/02 23:57
this-call: THIS :: did something
php-call: PHP :: did comething
this+php call: PHP :: did comething
this+php call: __call handler: 'dosomething2', array ()
To understand how this all works let's see how Blitz examines the call:
- When there is a namespace and a
::before function, Blitz checks if the namespace is a "hint". "Hints" are reserved namespaces:{{ php::do($params) }}, or{{ this::do($params) }}- When namespace is a "hint" ("php" or "this"). Having
{{ php::do($params) }}Blitz makes a static php-function call, having{{ this::do($param) }}do()method is called through the current template object. - When namespace is not a "hint", Blitz makes a static PHP-call
namespace::do($params). If this namespace and function exists - the function will be called.
- When namespace is a "hint" ("php" or "this"). Having
- When there was just a function call with no namespace like
{{ do($params) }}Blitz works according to priority settings. Whenblitz.php_callbacks_firstis set to 1 Blitz first tries to make a static PHP-function call. If this function doesn't exist - Blitz calls this method through your template controller. Whenblitz.php_callbacks_firstis set to 0, vice versa.
Now we can go back to the example:
-
{{ php::date("Y/m/d H:i") }}- is a PHP-function call -
{{ this::doSomething() }}- is a "hinted"doSomething()method call through the View object$T -
{{ php::doSomething() }}- is a "hinted"doSomething()function call from the current function scope -
{{ doSomething() }}- is a function call from the current function scope becauseblitz.php_callbacks_firstwas set to 1 -
{{ doSomething2() }}- Blitz tries to finddoSomething2()function in the current scope becauseblitz.php_callbacks_firstwas set to1. There's no such function, so then Blitz makes a method call through the View object$T. This call is handled by__call()becausedoSomething2()method doesn't exist.