How to use Smarty resources
November 6, 2007
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 theregister_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 32 33 | <?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!
Posted in
content rss

November 7th, 2007 at 10:47 am
If I remember correctly, we used this feature together in one of our projects, but with TemplateLite, and there was a bug in registering resources other than file…
November 9th, 2007 at 1:17 pm
@Adam: THX for the first comment on my blog.
Yes we used this feature together, but this solution works perfectly with Template Lite too.
November 22nd, 2008 at 10:30 pm
YES! Thank you so much! I spent hours trying to find a way to get smarty to read from a variable. I can’t thank you enough.
This is awesome!
August 23rd, 2009 at 8:20 pm
Thanks for the solution. It saved me some writing
August 23rd, 2009 at 8:44 pm
Btw, if we want the resource to be auto registered and save “$smarty->register_resource(…)” call, the functions must be named like this
smarty_resource_text_source
smarty_resource_text_timestamp
smarty_resource_text_secure
smarty_resource_text_trusted
and put in file resource.text.php in plugins dir.
September 10th, 2009 at 10:32 pm
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
October 26th, 2009 at 8:06 pm
Hello,
I have been written a php code for string resource before bus there was a problem that it repeats the last content in text resource when smarty force_compile is on.
Today I use yours and the problem is solved.
Thank you so veyyyyy much.
March 28th, 2010 at 4:12 pm
Thanks friend, you helped me alot!
April 18th, 2010 at 10:50 am
[...] http://blog.felho.hu/how-to-use-smarty-resources.html [...]