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

Bug: $smarty.const inside string

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


Joined: 03 Nov 2009
Posts: 72

PostPosted: Tue Mar 13, 2012 12:47 pm    Post subject: Bug: $smarty.const inside string Reply with quote

Latest Smarty version from SVN

Code:
PHP:
define('my_const', '1');

Smarty:
{$smarty.const.my_const}
{"{$smarty.const.my_const}"}

Output:
1
@my_const

Expected output:
1
1
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 13, 2012 6:11 pm    Post subject: Reply with quote

This was a sideefect of yesterdays fix.

The SVN trunk is updated again.
Back to top
View user's profile Send private message
ilyalyu
Smarty Regular


Joined: 03 Nov 2009
Posts: 72

PostPosted: Wed Mar 14, 2012 8:06 am    Post subject: Reply with quote

Thx Smile
Back to top
View user's profile Send private message
shoty
Smarty n00b


Joined: 28 Jan 2015
Posts: 1

PostPosted: Wed Jan 28, 2015 12:19 pm    Post subject: behavior changed in r4582 Reply with quote

in revision 4582 (string) cast was added to variables. (Revision comment: - correction of yesterdays bugfix (Forum Topic 21175 and 21182))
I have a function like this:

Code:
{reorder key="{$order_key}" by="company_id" label={language key="partner"} filter="list" filterlist="{$companies}"}


The $companies variable is an array. It used to work in older version (3.1.2).
In 3.1.21 no loger works.

Old compiled code:
Code:
<?php echo $_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['reorder'][0][0]->smarty_reorder(array('key'=>($_smarty_tpl->tpl_vars['order_key']->value),'by'=>"company_id",'label'=>$_tmp1,'filter'=>"list",'filterlist'=>($_smarty_tpl->tpl_vars['companies']->value)),$_smarty_tpl);?>


New compiled code:

Code:
<?php echo $_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['reorder'][0][0]->smarty_reorder(array('key'=>((string)$_smarty_tpl->tpl_vars['order_key']->value),'by'=>"company_id",'label'=>$_tmp1,'filter'=>"list",'filterlist'=>((string)$_smarty_tpl->tpl_vars['companies']->value)),$_smarty_tpl);?>


How can I pass that variable as an array ?

If I remove the quotes from variable still doesn't work.
Compiled code for this try:
Code:
<?php ob_start();?><?php echo $_smarty_tpl->tpl_vars['companies']->value;?>
<?php $_tmp2=ob_get_clean();?><?php echo $_smarty_tpl->smarty->registered_plugins[Smarty::PLUGIN_FUNCTION]['reorder'][0][0]->smarty_reorder(array('key'=>((string)$_smarty_tpl->tpl_vars['order_key']->value),'by'=>"company_id",'label'=>$_tmp1,'filter'=>"list",'filterlist'=>$_tmp2),$_smarty_tpl);?>


I just realised that if I also remove the { and } arround $companies variable it works as expected. It's just an unexpected behavior change and I have to verify a lot of template files to change this kind of code! I will let this comment here for others.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jan 28, 2015 12:48 pm    Post subject: Reply with quote

You were using a bug. The {$var} syntax casts the variable as string for printing in the document.
As you've discovered yourself, if you use variable directly, as you should, you have the expected functionality.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jan 29, 2015 7:02 pm    Post subject: Reply with quote

Attribute specifications like
Code:
foo = "{$bar}"

is always bad syntax.

Template variables must be passed like
Code:
foo = $bar

This syntax does work for all variable types.

Only if you want embed a string variable into another string you can use something like
Code:
foo = "my name is {$name}"
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 29, 2015 10:23 pm    Post subject: Reply with quote

U.Tews wrote:
Only if you want embed a string variable into another string you can use something like
Code:
foo = "my name is {$name}"

Even then, it's a bad juju…
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