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 and arrays

 
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
FreeSpace
Smarty n00b


Joined: 28 Jun 2004
Posts: 3
Location: Kiev, Ukraine

PostPosted: Mon Jun 28, 2004 3:27 pm    Post subject: default_modifiers and arrays Reply with quote

I'm not sure if this is default_modifiers bug, or just an "escape" modifier bug, but it seems that default_modifiers are applied to any template variable, even the arrays.
Here's an example:
bug.php
[php:1:212b8c5d00]<?php
require_once 'smarty/Smarty.class.php';
$Template = new Smarty;
$Template->default_modifiers[] = 'escape';

$data = array
(
'en' => array
(
'title' => 'English title',
'content' => 'English content'
),
'ru' => array
(
'title' => 'Russian title',
'content' => 'Russian content',
)
);

$Template->assign('data', $data);
$Template->display('bug.tpl');
?>[/php:1:212b8c5d00]
bug.tpl:
Code:

{foreach key=language_code item=language_data from=$data}
    {$language_code}:<br />
    {$language_data.title} - {$language_data.content}<br />
    <hr />
{/foreach}

Output:
Code:

<br />
<b>Warning</b>:  htmlspecialchars() expects parameter 1 to be string, array given in <b>/bla/bla/bla/smarty/plugins/modifier.escape.php</b> on line <b>25</b><br />
<br />
<b>Warning</b>:  htmlspecialchars() expects parameter 1 to be string, array given in <b>/bla/bla/bla/smarty/plugins/modifier.escape.php</b> on line <b>25</b><br />
    en:<br />

     - <br />
    <hr />
    ru:<br />
     - <br />
    <hr />

Engine tries to apply "escape" modifier on multidimensional array during foreach.
The simplies way to fix this is to open plugins/modifier.escape.php and just before
[php:1:212b8c5d00]<?php
switch ($esc_type) {
case 'html':
?>[/php:1:212b8c5d00]
add
[php:1:212b8c5d00]<?php
if (is_array($string)) {
return $string;
}
?>[/php:1:212b8c5d00]

Is this a bug or a feature anyway? Smile
_________________
Programming is the only art that drives combination of beautifulness and usefulness to it's highest level.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Mon Jun 28, 2004 10:25 pm    Post subject: Reply with quote

Discussed here:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=2662
Back to top
View user's profile Send private message
FreeSpace
Smarty n00b


Joined: 28 Jun 2004
Posts: 3
Location: Kiev, Ukraine

PostPosted: Mon Jun 28, 2004 11:01 pm    Post subject: Reply with quote

Thanks for the reply!
I read that thread, but I still can't get engine's logic.
My opinion is that it would be more logical not to modify arrays or objects with default_modifiers at all, since there's no default (meaning out-of-the-box) modifiers that can handle such a complex types, as far as I know.
If I'm wrong, would you please explain the sense of such behaviour?
_________________
Programming is the only art that drives combination of beautifulness and usefulness to it's highest level.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Mon Jun 28, 2004 11:11 pm    Post subject: Reply with quote

FreeSpace wrote:
If I'm wrong, would you please explain the sense of such behaviour?


Actually, I can't see the point of default modifiers at-all, at-all. Smile They don't really work the way anyone wants and there doesn't seem to be a good way to fix it in a general way; each "fix" seems to apply only for a particular situation). Further, with such a limited use-scenario, I find them a bit of a put-off and perhaps even a waste of engine space.

Much better in my mind to use custom functions in your classes and filter assigns through them as required instead of leaving that application type logic to Smarty.
Back to top
View user's profile Send private message
FreeSpace
Smarty n00b


Joined: 28 Jun 2004
Posts: 3
Location: Kiev, Ukraine

PostPosted: Tue Jun 29, 2004 12:37 am    Post subject: Reply with quote

Thanks for an advice!
I'll try to be more carefull and code withour these default modifiers Smile
Or I'll just extend original class if I'll had to.
_________________
Programming is the only art that drives combination of beautifulness and usefulness to it's highest level.
Back to top
View user's profile Send private message Visit poster's website
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