Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

text with {$variable} inside a mysql database

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bigethan
Smarty Rookie


Joined: 16 Jul 2003
Posts: 6

PostPosted: Wed Jul 16, 2003 5:21 pm    Post subject: text with {$variable} inside a mysql database Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jul 16, 2003 5:58 pm    Post subject: Reply with quote

http://smarty.php.net/manual/en/templates.from.elsewhere.php
or
http://smarty.php.net/manual/en/language.function.eval.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jul 16, 2003 6:58 pm    Post subject: Reply with quote

Sounds odd that you need Smarty to parse a stored HREF URL.
Back to top
View user's profile Send private message
bigethan
Smarty Rookie


Joined: 16 Jul 2003
Posts: 6

PostPosted: Thu Jul 17, 2003 4:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jul 17, 2003 5:56 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 17, 2003 6:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Jul 17, 2003 11:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Jul 18, 2003 7:52 am    Post subject: Reply with quote

@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
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Jul 18, 2003 8:19 am    Post subject: Reply with quote

@messju: hear hear! I am a nut for resources, though Wink

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 Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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