Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Rendering string as template (.tpl file)

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sat Mar 17, 2012 6:25 am    Post subject: Rendering string as template (.tpl file) Reply with quote

How can I render a string as normal template?
In my case, I have a $template PHP variable, that is the contents of a .tpl file stored in database or somewhere else.

To display that template, I need to treat it first as a normal template.

snapshot: eg.

Code:

<?php
$template = "
Hello {$id|name},
Thank you for your registration, ....
Your membership will expire in {$expiry}.
...
";
?>


Is there a way to handle/render this?
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Mar 17, 2012 9:16 am    Post subject: Reply with quote

Smarty3 has two build in resource types for it. "string" and "eval".

Code:

$template = 'some template source....';
$smarty->display("string:$template");
$smarty->display("eval:$template");


The string resource does store the compiled template code and it does create also a cache file when caching is enabled. Note that it creates a separate compiled and cache file for each source string. So it should be used only if you have reoccuring strings.

The eval resource will not save compiled code, so it calls the compiler on every single call. So it has much less performance but is usefull if the strings never look teh same.

You can use these resources also as subtemplates.

Code:
{include "string;$foo"}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP