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

Numbers and concat does not work

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
yankee
Smarty Rookie


Joined: 02 Mar 2009
Posts: 31

PostPosted: Fri Mar 06, 2009 4:53 pm    Post subject: Numbers and concat does not work Reply with quote

This template:
Code:
{$a*2&'asdf'}

leads to the following ouput:
Code:
<?php echo $_smarty_tpl->getVariable('a')->value*2.'asdf';?>

Which does not work, since php thinks that the dot introduces a floating point number.

This patch will set braces at need:
Code:
Index: Smarty3Alpha/libs/sysplugins/internal.templateparser.php
===================================================================
--- Smarty3Alpha/libs/sysplugins/internal.templateparser.php    (revision 3021)
+++ Smarty3Alpha/libs/sysplugins/internal.templateparser.php    (working copy)
@@ -1726,7 +1726,12 @@
     function yy_r39(){ $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor . $this->yystack[$this->yyidx + -1]->minor . $this->yystack[$this->yyidx + 0]->minor;     }
 #line 1732 "internal.templateparser.php"
 #line 216 "internal.templateparser.y"
-    function yy_r40(){ $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor . '.' . $this->yystack[$this->yyidx + 0]->minor;     }
+    function yy_r40(){
+      if (is_numeric(substr($this->yystack[$this->yyidx + -2]->minor, -1)))
+        $this->_retvalue = '('.$this->yystack[$this->yyidx + -2]->minor . ').' . $this->yystack[$this->yyidx + 0]->minor;
+      else
+        $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor .'.'. $this->yystack[$this->yyidx + 0]->minor;
+      }
 #line 1735 "internal.templateparser.php"
 #line 249 "internal.templateparser.y"
     function yy_r46(){ $this->_retvalue = "'".$this->yystack[$this->yyidx + -1]->minor."'";     }

But I fear that it's not the point of an automtic parser generator to edit it's output Wink.
I suppose the issue could be fixed by rebuilding the parser using the following patch:
Code:
Index: Smarty3Alpha/libs/lexer/internal.templateparser.y
===================================================================
--- Smarty3Alpha/libs/lexer/internal.templateparser.y   (revision 3021)
+++ Smarty3Alpha/libs/lexer/internal.templateparser.y   (working copy)
@@ -213,7 +213,7 @@
                                                                        // arithmetic expression
 exprs(res)        ::= exprs(e) math(m) value(v). { res = e . m . v; }
                                                                        // catenate
-exprs(res)        ::= exprs(e) ANDSYM value(v). { res = e . '.' . v; }
+exprs(res)        ::= exprs(e) ANDSYM value(v). { res = '('.e.')' . '.' . v; }

 //
 // mathematical operators

It will set braces even when they are not needed, but I can't see to much harm in this behaviour right now.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Mar 06, 2009 5:24 pm    Post subject: Reply with quote

Many thanks for your input.

The patch is needed also for the right side of '&'.

Is fixed in the SVN.

Regards Uwe
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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