View previous topic :: View next topic |
Author |
Message |
bigethan Smarty Rookie
Joined: 16 Jul 2003 Posts: 6
|
Posted: Wed Jul 16, 2003 5:21 pm Post subject: text with {$variable} inside a mysql database |
|
|
Hello.
I have some text in a database in the format of:
Code: | Please <a href="{$clickURL}">Click Here</a> |
I set up the {$clickURL} value, pull the text out of the database, and assign it to a smarty value:
Code: | $smarty->assign("clickURL", "http://www.yahoo.com");
$text = mysql_one_data("SELECT link_phrase from links where id = 1");
$smarty->assign("text", $text); |
and then in my template I insert the {$text} variable:
Code: | Hello and welcome to my website. {$text}. |
However the {$text} variable prints out exactly what was in the database without parsing the {$clickURL} value:
Code: | Hello and welcome to my website. Please <a href="{$clickURL}">Click Here</a> |
Is there a way to have the {$clickURL} value properly parsed? Or should I investigate other options? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 16, 2003 6:58 pm Post subject: |
|
|
Sounds odd that you need Smarty to parse a stored HREF URL. |
|
Back to top |
|
bigethan Smarty Rookie
Joined: 16 Jul 2003 Posts: 6
|
Posted: Thu Jul 17, 2003 4:48 pm Post subject: |
|
|
ah thanks. I did search around, and found links to calling templates from elsewhere, but that seemed like a lot of code for just a little thing. eval is just what I needed. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jul 17, 2003 5:56 pm Post subject: |
|
|
but (if you don't have smarty's caching enabled) eval has to compile your variable each time it is to be displayed (just like a little template). this is dog slown.
if you can manage to str_replace or preg_replace the values instead of involving smarty's compiler here, you may be a few hundred times faster. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jul 17, 2003 6:55 pm Post subject: |
|
|
Following messju, the template var modifier, regex_replace, is another option. I still think it is strange to need to do ANYTHING to a stored URL value. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Jul 17, 2003 11:25 pm Post subject: |
|
|
bigethan, it occurred to me that you may want to take a look at this resource which I describe here as it basically allows you to treat any field in your database as a template source.
Its generality is particularly handy during prototyping or to quickly Smarty enable an existing database. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Jul 18, 2003 7:52 am Post subject: |
|
|
@boots: forget about it. people don't want to code a resource (which is 4 functions with maybe about 10 lines of code altogether, depending on your db-abstraction-layer). they think eval is less typing. they don't want to know what a resource gains you in the long run, they don't even want to know the differences between the two. they think "eval" gets them faster to their goal. i think eval is *really* harmful. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Jul 18, 2003 8:19 am Post subject: |
|
|
@messju: hear hear! I am a nut for resources, though
I think it would be interesting to create a tyranical version of Smarty that stripped out all of the, err, uhm, EVIL features like eval and php and php_include and a few others.
Its a shame that the separation that Smarty provides is so quickly lost to lazy coding. Without those features, actual application design becomes more relevant--indeed, essential. As should be the case when using something like Smarty. Otherwise, all you are getting over straight PHP is caching and a lot of strained code.
Its ironic that the most 'powerful' features of Smarty are also its biggest traps.
**note: I'm not actually suggesting removing these features from Smarty. They have their place and I know that they have their fans |
|
Back to top |
|
|