String Template Resources

Smarty can render templates from a string by using the string: or eval: resource.

Note

With a string: resource type, each unique string generates a compiled file. Smarty cannot detect a string that has changed, and therefore will generate a new compiled file for each unique string. It is important to choose the correct resource so that you do not fill your disk space with wasted compiled strings.

Example 16.5. Using templates from strings

<?php
$smarty->assign('foo','value');
$template_string = 'display {$foo} here';
$smarty->display('string:'.$template_string); // compiles for later reuse
$smarty->display('eval:'.$template_string); // compiles every time
?>

  

From within a Smarty template

{include file="string:$template_string"} {* compiles for later reuse *}
{include file="eval:$template_string"} {* compiles every time *}


  

Both string: and eval: resources may be encoded with urlencode() or base64_encode(). This is not necessary for the usual use of string: and eval:, but is required when using either of them in conjunction with Extends Template Resource

Example 16.6. Using templates from encoded strings

 
 <?php
 $smarty->assign('foo','value');
 $template_string_urlencode = urlencode('display {$foo} here');
 $template_string_base64 = base64_encode('display {$foo} here');
 $smarty->display('eval:urlencode:'.$template_string_urlencode); // will decode string using urldecode()
 $smarty->display('eval:base64:'.$template_string_base64); // will decode string using base64_decode()
 ?>
 
   

From within a Smarty template

 
 {include file="string:urlencode:$template_string_urlencode"} {* will decode string using urldecode() *}
 {include file="eval:base64:$template_string_base64"} {* will decode string using base64_decode() *}