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

default modifiers applying twice?

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Sun Nov 28, 2004 4:54 pm    Post subject: default modifiers applying twice? Reply with quote

Despite this subject is very well related to this topic, I think it is worth to post a new thread about this.

In my way to manage using the default modifiers feature successfully, I think I have found a case where these modifiers are applied twice, so the HTML escaping is not being made correctly.

I have created a testcase for you to try it, it consists of 2 files, which must be placed on the same directory. Below is the code for the files:

bug.php
Code:

<?php

  require_once '../../libs/Smarty.class.php';

  class MyClass{

    var $sLines;

    function MyClass(){
      $this->sLines = "hello\r\n we are starsky & hutch\r\nbye bye";
    }

    function GetLines(){
      return explode("\r\n",$this->sLines);
    }

  }

  function CustomEscape($vSmartyVariable){
    if (is_string($vSmartyVariable))
      return htmlentities($vSmartyVariable);

    return $vSmartyVariable;
  }

  $object = new MyClass();

  $oSmarty = new Smarty();

  $oSmarty->default_modifiers = array('CustomEscape');
  $oSmarty->template_dir = './';

  $oSmarty->assign('object',$object);

  $oSmarty->display('bug.tpl');

?>


bug.tpl:
Code:

<html>
  <head>
    <title>Testcase for possible Smarty bug</title>
  </head>
  <body>

        {foreach from=$object->GetLines() item=sLine}
        <h1>{$sLine}</h1>
        {/foreach}

  </body>
</html>



The result in the rendered page is the text "we are starsky &amp;amp; hutch" instead of "we are starsky &amp; hutch", which would be the expected result.

Any comments on these would be greatly appreciated.
Back to top
View user's profile Send private message
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Fri Dec 10, 2004 10:13 am    Post subject: Reply with quote

No answers to this? Messju, as you see on my comments on the first thread about this topic, I *know* that default_modifiers is being applied also on any simple operation (including non-printing operations, like comparing variables in if statements), but I think there is a bug because here the default_modifier is applying twice, but not only "twice=two times", here "twice" means: applying once and use the result of the first function as the parameter for the second applyment of the default_modifier function, which I think is a bug.

Please comment anything about this! Heeelppp...

Many thanks to all.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Dec 10, 2004 2:39 pm    Post subject: Reply with quote

Default modifiers are applied to all variables in the template, printed or not. That is because at the stage the modifiers are applied to the variable, it is unknown how they are used in the template. For your needs, default modifiers many not be the best way to go. Maybe you could create a prefilter that finds your variables and appends the modifier to them. ( replace {$foo} with {$foo|escape} )
Back to top
View user's profile Send private message Visit poster's website
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Fri Dec 10, 2004 3:11 pm    Post subject: Reply with quote

Quote:
Default modifiers are applied to all variables in the template, printed or not


Yes, I know that, please re-read my second post.

I am not complaining about default_modifiers being applied twice, I am complaining about being applied RECURSIVELY twice, I mean:

Not this: {some_function $var|def_mod}{other_function $var|def_mod}
But this: {some_function $var|def_mod|def_mod}

I don't know if I am explaining it correctly. I mean that, despite default_modifiers get applied always (in print operations or non-print operations), I have been using them without trouble EXCEPT for this case, in which they are applied recursively.

Perhaps if the variable in the "from" attribute of the foreach function hadn't a method call, the block would work, I don't know, I have to make some tests.

Regards,

knocte
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Dec 10, 2004 3:19 pm    Post subject: Reply with quote

Try this:

Code:
{foreach from=$object->GetLines()|smarty:nodefaults item=sLine}
   <h1>{$sLine}</h1>
{/foreach}


Or just don't use defaults:

Code:
{foreach from=$object->GetLines() item=sLine}
   <h1>{$sLine|escape}</h1>
{/foreach}
Back to top
View user's profile Send private message Visit poster's website
knocte
Smarty Rookie


Joined: 30 Nov 2003
Posts: 23
Location: Spain, Europe

PostPosted: Sat Dec 11, 2004 6:19 pm    Post subject: Reply with quote

Thanks for your reply man, but I prefer to help solving a bug than applying a workaround on my project's code. I mean that I already knew the existence of smarty:nodefaults...
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 -> Bugs 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