|
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 |
leanweb Smarty Rookie
Joined: 27 Oct 2004 Posts: 24 Location: Newark, DE
|
Posted: Thu Oct 28, 2004 12:12 am Post subject: should attributes with no value be a syntax error? |
|
|
i'm working on a plugin that would greatly benefit from the following syntax:
{plugin action1 param1="value1" param2="value2"}
{plugin action2 param1="value1" param2="value2"}
meaning that the first attribute would act as opcode followed by zero or more parameters.
now, of course there are workarounds, for example one could do
{plugin_action1 param1="value1" param2="value2"}
{plugin_action2 param1="value1" param2="value2"}
turning one plugin into serveral or
{plugin command="action1" param1="value1" param2="value2"}
{plugin command="action2" param1="value1" param2="value2"}
but i think something has to be said for code usability.
yet, the interpretation of syntax that i'm proposing should be unambigous enough. an attribute with no value should be interpreted as boolean true.
all that would have to change in Smarty_Compiler.class.php are the following lines in _parse_attrs() function:
[php:1:4995a30e67]<?php
/**
* Parse attribute string
*
* @param string $tag_args
* @return array
*/
function _parse_attrs($tag_args)
{
/* Tokenize tag attributes. */
preg_match_all('~(?:' . $this->_obj_call_regexp . '|' . $this->_qstr_regexp . ' | (?>[^"\'=\s]+)
)+ |
[=]
~x', $tag_args, $match);
$tokens = $match[0];
$attrs = array();
/* Parse state:
0 - expecting attribute name
1 - expecting '='
2 - expecting attribute value (not '=') */
$state = 0;
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 == '=') {
$state = 2;
} else {
$attrs[$attr_name] = true;
$state = 0;
// $this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
}
break;
case 2:
/* If token is not '=', we set the attribute value and go to
state 0. */
if ($token != '=') {
/* We booleanize the token if it's a non-quoted possible
boolean value. */
if (preg_match('~^(on|yes|true)$~', $token)) {
$token = 'true';
} else if (preg_match('~^(off|no|false)$~', $token)) {
$token = 'false';
} else if ($token == 'null') {
$token = 'null';
} else if (preg_match('~^-?([0-9]+|0[xX][0-9a-fA-F]+)$~', $token)) {
/* treat integer literally */
} else if (!preg_match('~^' . $this->_obj_call_regexp . '|' . $this->_var_regexp . '(?:' . $this->_mod_regexp . ')*$~', $token)) {
/* treat as a string, double-quote it escaping quotes */
$token = '"'.addslashes($token).'"';
}
$attrs[$attr_name] = $token;
$state = 0;
} else
$this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
break;
}
$last_token = $token;
}
/*
if($state != 0) {
if($state == 1) {
$this->_syntax_error("expecting '=' after attribute name '$last_token'", E_USER_ERROR, __FILE__, __LINE__);
} else {
$this->_syntax_error("missing attribute value", E_USER_ERROR, __FILE__, __LINE__);
}
}
*/
$this->_parse_vars_props($attrs);
return $attrs;
}?>[/php:1:4995a30e67]
what do you guys think? |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Thu Oct 28, 2004 6:09 am Post subject: |
|
|
Code: |
{plugin command="action1" param1="value1" param2="value2"}
{plugin command="action2" param1="value1" param2="value2"}
|
i think thats a good solution, too.
The only thing i would do is to change the attribute name from "command" to a short one, e.g. "cmd" or "mode" |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 28, 2004 2:10 pm Post subject: Re: should attributes with no value be a syntax error? |
|
|
This has been talked about before. Don't expect it. Just think of the example set by XML (and hence its applications, like XHTML)-- neither portion of a name/value pair is optional there either. For one thing, it makes parsing easier for both humans and machines (and makes detecting errors a bit more obvious as well).
leanweb wrote: | yet, the interpretation of syntax that i'm proposing should be unambigous enough. an attribute with no value should be interpreted as boolean true. |
More obvious is {foo bar=true}. Sure, it is kind of sexy, but remember that the templates are not intended for hardcore programmers, or at least, they must always be accessible to those who are not strictly coming from "technical" backgrounds.
You have other options other than fiddling with the internals including prefilters (to re-write your code to something that is Smarty compliant) or even compiler plugins which use a free-form input method. Another option is to implement your plugin as a modifier so that you needn't use named parameters at all.
That's just my 2c. |
|
Back to top |
|
leanweb Smarty Rookie
Joined: 27 Oct 2004 Posts: 24 Location: Newark, DE
|
Posted: Fri Oct 29, 2004 9:39 pm Post subject: |
|
|
thank you for explanation. |
|
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
|
|