How to use Smarty resources
Have you ever faced the situation, that the content of a variable is a Smarty template like string, and you want to fetch or display it? I worked on project where we decided that our language file entries will be Smarty templates, because we need the flexibility of the template engine (sprintf was not enough). The problem is, that by default Smarty’s fetch/display method accepts a template file name. Of course we could write the text to a temporary file, but it would be bigger hack than PHPNuke. Fortunately Smarty has the mechanism to solve this problem via defining custom resource types.
Smarty resources
Smarty supports several type of plug-ins including resource plug-ins. Resource plug-ins allow you to change the way Smarty looks for templates. For example you can create a db resource plug-in, which allows you to store your templates in database. To use this plug-in you should refer to the templates with a db: prefix. For example $smarty->display('db:login.tpl'); or in templates {include file='db:header.tpl'}. The plug-in will find the template in the database, and return it.
Register a Smarty resource plug-in
You can register a custom Smarty resource plug-in with the register_resource() method:
1 2 3 4 5 6 7 8 | <?php $smarty->register_resource('db', array( 'db_get_template', 'db_get_timestamp', 'db_get_secure', 'db_get_trusted') ); ?> |
The first parameter is the name of the resource (should be at least two characters in length) and the second is an array with four function name which implement the plug-in. Every function will receive the requested resource as the first parameter and the Smarty object as the last parameter. The rest of parameters depend on the function.
From the Smarty manual:
1 2 3 4 5 6 7 | bool smarty_resource_name_source (string $rsrc_name, string &$source, object &$smarty) bool smarty_resource_name_timestamp (string $rsrc_name, int &$timestamp, object &$smarty) bool smarty_resource_name_secure (string $rsrc_name, object &$smarty) bool smarty_resource_name_trusted (string $rsrc_name, object &$smarty) |
- The first function, source() is supposed to retrieve the resource. Its second parameter $source is a variable passed by reference where the result should be stored. The function is supposed to return TRUE if it was able to successfully retrieve the resource and FALSE otherwise.
- The second function, timestamp() is supposed to retrieve the last modification time of the requested resource, as a UNIX timestamp. The second parameter $timestamp is a variable passed by reference where the timestamp should be stored. The function is supposed to return TRUE if the timestamp could be successfully determined, or FALSE otherwise.
- The third function, secure()is supposed to return TRUE or FALSE, depending on whether the requested resource is secure or not. This function is used only for template resources but should still be defined.
- The fourth function, trusted() is supposed to return TRUE or FALSE, depending on whether the requested resource is trusted or not. This function is used for only for PHP script components requested by {include_php} tag or {insert} tag with the src attribute. However, it should still be defined even for template resources.
You can find on the manual page a possible implementation of the db resource plugin.
Solution for our original problem: text resource
We can create a custom text resource to solve the problem mentioned in the introduction. The necessary code will be very straightforward:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php function smarty_resource_text_get_template($tpl_name, &$tpl_source, &$smarty_obj) { $tpl_source = $tpl_name; return true; } function smarty_resource_text_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) { $tpl_timestamp = time(); return true; } function smarty_resource_text_get_secure($tpl_name, &$smarty_obj) { return true; } function smarty_resource_text_get_trusted($tpl_name, &$smarty_obj) {} include('Smarty-2.6.18/libs/Smarty.class.php'); $smarty = new Smarty(); $smarty->register_resource('text', array( 'smarty_resource_text_get_template', 'smarty_resource_text_get_timestamp', 'smarty_resource_text_get_secure', 'smarty_resource_text_get_trusted') ); $smarty->assign('foo', 'world'); echo $smarty->fetch('text:Hello {$foo}!'); // Hello world! ?> |
Smarty resources could be very useful in some situations, keep in mind this feature!
source : http://blog.felho.hu/how-to-use-smarty-resources.html
Popularity: 6% [?]
Tags: Smarty Home




