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

unnamed arguments with custom function

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
wkpark
Smarty n00b


Joined: 02 Jun 2009
Posts: 1

PostPosted: Tue Jun 02, 2009 2:03 am    Post subject: unnamed arguments with custom function Reply with quote

how about to support unnamed arguments, like as

{hello "Hello" "World"}

or

{err "ERROR" `$code`}

etc.

this is a simple patch to support it.

Code:

--- a/Smarty_Compiler.class.php   2009-06-02 09:59:38.000000000 +0900
+++ b/Smarty_Compiler.class.php     2009-06-02 10:28:49.000000000 +0900
@@ -1540,16 +1566,6 @@

         foreach ($tokens as $token) {
             switch ($state) {
-                case 0:
-                    /* If the token is a valid identifier, we set attribute name
-                       and go to state 1. */
-                    if (preg_match('~^\w+$~', $token)) {
-                        $attr_name = $token;
-                        $state = 1;
-                    } else
-                        $this->_syntax_error("invalid attribute name: '$token'", E_USER_ERROR, __FILE__, __LINE__);
-                    break;
-
                 case 1:
                     /* If the token is '=', then we go to state 2. */
                     if ($token == '=') {
@@ -1557,7 +1573,17 @@
                     } else
                         $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
                     break;
-
+                case 0:
+                    /* If the token is a valid identifier, we set attribute name
+                       and go to state 1. */
+                    if (preg_match('~^\w+$~', $token)) {
+                        $attr_name = $token;
+                        $state = 1;
+                        break;
+                    } else { // unnamed args. continue
+                        $attr_name = '';
+                        $state = 2;
+                    }
                 case 2:
                     /* If token is not '=', we set the attribute value and go to
                        state 0. */
@@ -1577,7 +1603,11 @@
                             $token = '"'.addslashes($token).'"';
                         }

-                        $attrs[$attr_name] = $token;
+                        if ($attr_name) {
+                                $attrs[$attr_name] = $token;
+                        } else {
+                                $attrs[] = $token;
+                        }
                         $state = 0;
                     } else
                         $this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);

-----
unnamed args are simple and intuitively for human (not for xml/xhtml)

with this support you can use user defined fuctions more intuitively like as

{math "2+4*12"}, {sort "a" "b" "c" "d"} and more

-----
See also:
* osdir dot com / ml / php dot smarty dot devel /2003-12/msg00050.html
* osdir dot com / ml / php dot smarty dot devel /2003-12/msg00052.html


Last edited by wkpark on Tue Jun 02, 2009 5:07 am; edited 1 time in total
Back to top
View user's profile Send private message
Tae-young
Smarty n00b


Joined: 02 Jun 2009
Posts: 1

PostPosted: Tue Jun 02, 2009 3:53 am    Post subject: Reply with quote

This patch will make it easier to make some wrapper function like gettext _().

instead of using _("") in php code, we can use {_ "Some message"} in smarty with some _() wrapper plugin.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development 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