|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mercenary Smarty n00b
Joined: 05 Jun 2004 Posts: 3 Location: Osaka, Japan
|
Posted: Sat Jun 05, 2004 1:48 pm Post subject: foreach and $default_modifiers |
|
|
hi, list.
I'm seeing strange interaction between {foreach} and $default_modifiers.
in short: {foreach} applies $default_modifiers *twice*
I've google'd and read document, but cannot find anything.
so here goes my question.
first, my environment is:
smarty 2.6.2
apache 1.3.31
mod_php4 4.3.6
mod_ssl 2.8.18_3
Mozilla-1.6
now here's sample code.
[php:1:e3d763816d]<?php
define('SMARTY_DIR', '/dir/to/smarty/') ;
require_once( SMARTY_DIR.'Smarty.class.php' ) ;
$s = new Smarty() ;
$s->compile_dir = realpath(SMARTY_DIR.'../templates_c/');
$s->template_dir = realpath(SMARTY_DIR.'../templates/');
$s->default_modifiers = array('escape:"htmlall"');
$s->assign('ary', array('this should be seen as <html>'));
$s->display('test.tpl');
exit;
// NOTREACHED
?>[/php:1:e3d763816d]
and template
Code: |
{* test template for foreach begin *}
{foreach from=$ary item='fe'}
{$fe}
{/foreach}
<br />
{section name='sct' loop=$ary}
{$ary[sct]}
{/section}
{* test template for foreach end *}
|
here's output from mozilla.
Quote: |
this should be seen as &html&
this should be seen as <html>
|
as you can see, foreach's output looks like escaped *twice*.
BTW, section's output looks correct.
actually, I've checked compiled template and found that $default_modifier is applied to subject variable when count() is used to know how many times to loop, and applied again when variable is being output.
yes, I can use {section} instead of {foreach} to avoid this problem, but {foreach} is very convenient way to loop through array, and this behaviour looks incorrect to me.
can anyone confirm this ?
does anyone know how to workaround ?
sorry if this is known problem... and sorry for broken english.
thanks in advance.
mercenary
// |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
mercenary Smarty n00b
Joined: 05 Jun 2004 Posts: 3 Location: Osaka, Japan
|
Posted: Sat Jun 05, 2004 3:09 pm Post subject: |
|
|
oh, yes!!
that's really a solution.
thank you very much for pointing me out.
now I've changed template as following
Code: |
{* test template for foreach begin *}
{foreach from=$ary|smarty:nodefaults item='fe'}
{$fe}
{/foreach}
<br />
{section name='sct' loop=$ary}
{$ary[sct]}
{/section}
{* test template for foreach end *}
|
I get correct output:
Quote: |
this should be seen as <html>
this should be seen as <html>
|
# this is counter-intuitive, IMHO.
Is this a FAQ ?
thank you very much for very quick reply.
I didn't think I get a reply within an hour.
that's really geat !!
mercenary
// |
|
Back to top |
|
mercenary Smarty n00b
Joined: 05 Jun 2004 Posts: 3 Location: Osaka, Japan
|
Posted: Sat Jun 05, 2004 8:42 pm Post subject: |
|
|
hi, list.
followup to myself, I think I've found simular interaction between {if} and $default_modifier, as in {foreach} and $default_modifier.
the code:
[php:1:58c76a9d8b]<?php
define('SMARTY_DIR', '/dir/to/smarty/') ;
require_once( SMARTY_DIR.'Smarty.class.php' ) ;
$s = new Smarty() ;
$s->compile_dir = realpath(SMARTY_DIR.'../templates_c/');
$s->template_dir = realpath(SMARTY_DIR.'../templates/');
$s->default_modifiers = array('escape:"htmlall"');
class foo {
var $m_var;
};
$foo = new foo();
$foo->m_var = 'this should be seen as <html>';
$s->assign('foo', $foo);
$s->display('test.tpl');
exit;
// NOTREACHED
?>[/php:1:58c76a9d8b]
with following template, I get blank page.
Code: |
{if $foo}
{$foo->m_var}
{/if}
|
if I use following template(i.e. using smarty:nodefaults)
Code: |
{if $foo|smarty:nodefaults}
{$foo->m_var}
{/if}
|
and I get correct output as follows:
Quote: |
this should be seen as <html>
|
can anyone confirm this ?
and if this is the case, be careful if you're using object(not array) and $default_modifiers at the same time.
just FYI.
mercenary
// |
|
Back to top |
|
|
|
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
|