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 to html

 
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
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Mon Aug 11, 2003 2:41 pm    Post subject: text to html Reply with quote

I would like to hear your opinions, in which stage a text string should be converted to html (special chars, new lines, etc).

1. Before inserting it to the database
2. When setting the smarty variable
3. In smarty, using a modifier?

Obviously the first method is probably faster but takes more db space, and also limits you to html only (you cant export data as text).

The thing is - I'm converting a large ecommerce site to smarty. Right now we have HTML in the database but I think it might be wrong (for example if someone wants to export data to other programs, like flash). I thought that using a modifier is the most sensible solution, but since many designers will build templates for this system, I'm not sure that it'll be the right solution.

So which method are you using? did you experience problems with your designers when using modifiers (like forgetting to use it)? what is the overhead of converting it on every request?
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Aug 11, 2003 2:55 pm    Post subject: Reply with quote

For now I am using modifiers but I am not so satisfied because of their speed. Perhaps the best solution would be to escape your string before inserting it into db. If you use htmlentities() you can write an unhtmlentities() function which converts the text back to plain ascii before exporting.

Code:

    function unhtmlentities ($myVar, $quote_style = null) {
        if (is_null($quote_style)) $quote_style = ENT_COMPAT;
        $trans_tbl = get_html_translation_table (HTML_ENTITIES, $quote_style);
        $trans_tbl = array_flip ($trans_tbl);
        $myVar = strtr ($myVar, $trans_tbl);
        $myVar = preg_replace('/\&\#([0-9]+)\;/me', "chr('\\1')", $myVar);
        return $myVar;
    }

Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Mon Aug 11, 2003 2:59 pm    Post subject: Reply with quote

andre wrote:
For now I am using modifiers but I am not so satisfied because of their speed. Perhaps the best solution would be to escape your string before inserting it into db. If you use htmlentities() you can write an unhtmlentities() function which converts the text back to plain ascii before exporting.


Yes I know - that's what I'm doing now.

What is causing the slow speed with modifiers? is it faster if you run htmlspecialchars() when setting the smarty variable rather than using a modifier?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Mon Aug 11, 2003 3:19 pm    Post subject: Reply with quote

sagi wrote:
What is causing the slow speed with modifiers? is it faster if you run htmlspecialchars() when setting the smarty variable rather than using a modifier?


calling a modifier in a template is a bit slower than simply calling a function in php but the modifier-calling was optimized recently and should become considerably faster in the next release of smarty.

of cause converting the data once when writing it is faster than converting it on every read from the db but writing the data already formatted into the db can cause many problems if you want to edit the contents afterwards (via a web-frontend maybe) and (as you already pointed out) can cause problems if you want to publish the data in different formats.

i use the via-a-modifier-in-the-template approach. if the people writing the templates forget the modifiers we'll notice when testing the pages and fix it. i also try to employ smarty's caching where possible: when the page is cached the modifier's arent't called anymore, the page is simply read from the disk and dumped to the browser: with that i have all the convinience of modifiers and other stuff in the templates and have the convinience of the raw data in the db and don't have to bother if 2, 10 or 20 (slow or fast) modifiers are used on the page, they are called virtually once for each page and after that the page can be displayed thousands of times without any overhead anymore (i don't even have to connect the db!).

unfortunately caching is a bit tricky if you have highly dynamic parts (user configurable layout, a shopping cart etc. ) that you want to intermix with cached parts (like contents from the db that don't change often) and caching is hard to employ afterwards to a project, it has to be kept in mind in the design-phase already to get the best out of it with the least effort.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue Aug 12, 2003 12:09 pm    Post subject: Reply with quote

Good point, messju. Wink
Back to top
View user's profile Send private message
sagi
Smarty Regular


Joined: 30 Jul 2003
Posts: 43

PostPosted: Tue Aug 12, 2003 12:24 pm    Post subject: Reply with quote

I see, I think I'll use modifiers myself..

I understand that if I insert a block of text I muse use the nl2br modifier too? like {$text|escape|nl2br} ?
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