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

Auto-Escape Variables without default-modifiers

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
FirePanther
Smarty n00b


Joined: 30 Apr 2013
Posts: 3

PostPosted: Tue Apr 30, 2013 1:48 am    Post subject: Auto-Escape Variables without default-modifiers Reply with quote

Hi,
I want to auto-escape my variables, but i don't want to use default-modifiers, because these are to difficult to unescape.
Example:
I have the variable $test with the value "<b>test</b>".
When I use:
{$test}
it should print:
&lt;b&gt;test&lt;/b&gt;

And when i want to unescape it, I only have to put an @-char:
{@$var}

With this method it should be more difficult to make a mistake and forget the |escape.
I only need this method for printing, When I use {if $test} it doesn't have to be escaped.

I found the file smarty_internal_templateparser.php and the function yy_r122, but I have no plan how to realize it.

thank you Smile
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7362
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 30, 2013 2:34 am    Post subject: Reply with quote

this should help (smarty 3 only):

http://www.smarty.net/docs/en/variable.escape.html.tpl
Back to top
View user's profile Send private message Visit poster's website
FirePanther
Smarty n00b


Joined: 30 Apr 2013
Posts: 3

PostPosted: Tue Apr 30, 2013 3:44 pm    Post subject: Reply with quote

hi,
thank you, but i am searching a way to unescape with @$var, instead of $var nofilter.
i worked a long time with the woltlab wcf template parser.
they escape all variables automatically and unescape with an @ char.
but thank you for your post, i will try to edit smarty a bit.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7362
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 30, 2013 3:52 pm    Post subject: Reply with quote

If you insist on a unique syntax, save yourself some headache... create a prefilter that changes {@$foo} to {$foo nofilter} in the templates just before compile. editing smarty just creates trouble for upgrades later on.
Back to top
View user's profile Send private message Visit poster's website
FirePanther
Smarty n00b


Joined: 30 Apr 2013
Posts: 3

PostPosted: Tue Apr 30, 2013 4:02 pm    Post subject: Reply with quote

thank you, it works Smile
Code:
function at_var_unescape($tpl_source, &$smarty) {
   $tpl_source = preg_replace('~\{\@(\$[^\s\}]+)\}~', '{$1 nofilter}', $tpl_source);
   return $tpl_source;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons 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