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

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
Nilse
Smarty n00b


Joined: 15 Sep 2010
Posts: 2

PostPosted: Wed Sep 15, 2010 7:31 pm    Post subject: Parsing a string Reply with quote

Hi Smarty Community,
i have a string, which contains smarty tags. E.g.: "Hello {$username}"

I have made a new smarty function, which fetches this strings from a database... {getDBEntry id=1}

Is is possible to parse (evaluate) the database entry, BEFORE assigning it to the template?

Something like:
$smarty->parse($string);


$smarty->fetch() is similar, but it only takes templates files...

So, any ideas? Wink
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Sep 15, 2010 8:10 pm    Post subject: Reply with quote

With Smarty3 you can use the string resource.

$smarty->fetch('string:'.$string);
Back to top
View user's profile Send private message
Nilse
Smarty n00b


Joined: 15 Sep 2010
Posts: 2

PostPosted: Thu Sep 16, 2010 8:51 am    Post subject: Reply with quote

Thank you very much...
I did not know that I can use string: with the fetch function...

greetings
Nilse
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Tue Jan 29, 2013 7:03 pm    Post subject: Reply with quote

Might I suggest adding something about this "trick" to the Examples section of the Smarty Manual's fetch() page at http://www.smarty.net/docs/en/api.fetch.tpl ?

This is an incredibly useful capability in Smarty 3, yet how to do this is not obvious from the fetch() manual page. One must dig a bit further, which leads him to http://www.smarty.net/docs/en/resources.tpl , and eventually, http://www.smarty.net/docs/en/resources.string.tpl .

This forum post was the only way I figured out how to do this.

In any event, thanks for adding this capability!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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