How to use Smarty resources

Date 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!

9 Responses to “How to use Smarty resources”

  1. Adam said:

    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…

  2. Felho said:

    @Adam: THX for the first comment on my blog. :) Yes we used this feature together, but this solution works perfectly with Template Lite too.

  3. Benjamin said:

    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!

  4. Ventzy said:

    Thanks for the solution. It saved me some writing :)

  5. Ventzy said:

    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.

  6. sandrar said:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  7. Mojtaba said:

    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.

  8. Naveed Ahmed said:

    Thanks friend, you helped me alot!

  9. Whoila Blog » Blog Archive » Useful guide about SMARTY resources and examples said:

    [...] http://blog.felho.hu/how-to-use-smarty-resources.html [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>