|
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 |
ieure Smarty n00b
Joined: 14 Oct 2003 Posts: 3
|
Posted: Tue Oct 14, 2003 10:32 pm Post subject: Creating an array in Smarty |
|
|
Hi there. I'm using Smarty for a project, and I need to create an array inside a Smarty template, for processing by another template.
e.g.
{include file="Foo.tpl" arg=array("one", "two", "three")}
or similar. Is this possible? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Oct 14, 2003 10:51 pm Post subject: |
|
|
For a heated discussion, try Assign to arrays.
NB: includes Messju's simple append suggestion
You can roll your own. This hidden gem of a thread has some relevant code.
NB: includes Messju's cool compiler assign plugin
Another example using a different approach is in this thread populate an associative array in tpl.
EDIT: You can also exploit the fact that PHP functions can be used as modifiers, though it is a bit hacky, IMHO:
{include file="Foo.tpl" arg=","|explode:"one,two,three"}
HTH |
|
Back to top |
|
ieure Smarty n00b
Joined: 14 Oct 2003 Posts: 3
|
Posted: Wed Oct 15, 2003 11:08 pm Post subject: |
|
|
boots wrote: | For a heated discussion, try Assign to arrays.
NB: includes Messju's simple append suggestion
|
Thank you, this is perfect. |
|
Back to top |
|
roth_heiko Smarty n00b
Joined: 21 Sep 2006 Posts: 4 Location: Germany, Mosbach
|
Posted: Thu Sep 21, 2006 2:51 pm Post subject: associative array |
|
|
Hello Monte,
I send you a small patch which allows associative arrays as plugin params.
An example:
Code: | {navigation id=$site->rootId var=nav
query.fields="id,name,url,order_field" query.where="type='news/entry'"}
|
Plugin code:
Code: | function smarty_function_navigation($params, &$smarty)
{
$children = $nav->getChildren($params['query'], array('no_nav_hide'
=> true));
if (!$children) return false;
$smarty->assign($params['var'], $children);
}
|
without patch:
Code: | {navigation id=$site->rootId var=nav
query="array('fields'=>'id,name,url,order_field',
'where'=>'type=\'news/entry\''}
|
Plugin code:
Code: | function smarty_function_navigation($params, &$smarty)
{
$params['query'] = eval($params['query']); // This is evil.
$children = $nav->getChildren($params['query'], array('no_nav_hide'
=> true));
if (!$children) return false;
$smarty->assign($params['var'], $children);
}
|
or
Code: | {navigation id=$site->rootId var=nav
query_fields="id,name,url,order_field" query_where="type='news/entry'"}
|
drawback: You have to define and process every possible key.
Plugin code:
Code: | function smarty_function_navigation($params, &$smarty)
{
$params['query'] = array();
if (isset($params['query_order']))
{
$params['query']['order'] = $params['query_order'];
}
if (isset($params['query_fields']))
{
$params['query']['fields'] = $params['query_fields'];
}
if (isset($params['query_where']))
{
$params['query']['where'] = $params['query_where'];
}
if (isset($params['query_from']))
{
$params['query']['from'] = $params['query_from'];
}
$children = $nav->getChildren($params['query'], array('no_nav_hide'
=> true));
if (!$children) return false;
$smarty->assign($params['var'], $children);
}
|
We define Smarty plugins as an interface to our framework. With the
associative notation described above, we can extend our framework and
the extension can be used by our designers without changing the Smarty
plugin.
Another benefit is that the plugin code is much smaller.
This patch is completly compatible with all plugins. We use it for
several months now.
Please consider to bring the patch to 2.6.15
This patch works multidimensional, too.
Thanks,
Heiko.
Code: | Index: Smarty_Compiler.class.php
===================================================================
--- Smarty_Compiler.class.php (Revision 23425)
+++ Smarty_Compiler.class.php (Revision 23427)
@@ -1529,7 +1529,7 @@
case 0:
/* If the token is a valid identifier, we set attribute name
and go to state 1. */
- if (preg_match('~^\w+$~', $token)) {
+ if (preg_match('~^[\w\.]+$~', $token)) {
$attr_name = $token;
$state = 1;
} else
@@ -1562,8 +1562,19 @@
/* treat as a string, double-quote it escaping quotes */
$token = '"'.addslashes($token).'"';
}
-
- $attrs[$attr_name] = $token;
+ if (strpos($attr_name, '.')===false)
+ {
+ $attrs[$attr_name] = $token;
+ } else { // Input of associative arrays with notation test.foo="4711" test.foo2="water" becomes $params[test][foo]="4711" and $params[test][foo2]="water"
+ $attr_name2 = explode('.', $attr_name);
+ $attrs2 = $token;
+ while ($attr_name3 = array_pop($attr_name2))
+ {
+ $attrs3 = array($attr_name3 => $attrs2);
+ $attrs2 = $attrs3;
+ }
+ $attrs = array_merge_recursive($attrs, $attrs2);
+ }
$state = 0;
} else
$this->_syntax_error("'=' cannot be an attribute value", E_USER_ERROR, __FILE__, __LINE__);
@@ -1608,6 +1619,15 @@
*/
function _parse_var_props($val)
{
+ if (is_array($val))
+ { // Associative arrays.
+ $a = array();
+ foreach ($val as $key => $value)
+ {
+ $a[] = "'$key' => ".$this->_parse_var_props($value);
+ }
+ return 'array('.join(',', $a).')';
+ }
$val = trim($val);
if(preg_match('~^(' . $this->_obj_call_regexp . '|' . $this->_dvar_regexp . ')(' . $this->_mod_regexp . '*)$~', $val, $match)) {
|
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Oct 05, 2006 2:17 pm Post subject: |
|
|
Hi roth_heiko.
I think that this feature is better for discussion in 3.x. I don't see it as that important for a template engine -- at least in the core -- but obviously some do |
|
Back to top |
|
roth_heiko Smarty n00b
Joined: 21 Sep 2006 Posts: 4 Location: Germany, Mosbach
|
Posted: Mon Oct 23, 2006 3:46 pm Post subject: |
|
|
boots wrote: | I think that this feature is better for discussion in 3.x. I don't see it as that important for a template engine -- at least in the core -- but obviously some do |
I think, it's not a big patch, but a core patch, ok.
But it's very useful, at least for us, because we use many associative, multidimensional arrays.
I can patch every Smarty version, but I thought it's worth a look and an integration.
And speaking of version 3, is there a fork?
I would like to contribute. Speed could be very much improved. |
|
Back to top |
|
shannera Administrator
Joined: 13 Feb 2006 Posts: 802 Location: Edertal, Germany
|
Posted: Mon Oct 23, 2006 4:03 pm Post subject: |
|
|
roth_heiko wrote: |
And speaking of version 3, is there a fork?
I would like to contribute. Speed could be very much improved. |
In this thread (http://www.phpinsider.com/smarty-forum/viewtopic.php?t=4989) you can see the actual status of version 3, summarized: a bunch of ideas, some nice, some not, but no work is done yet. But I'm sure the lead programmers are interested in any patch that speed up smarty the actual smarty very much. |
|
Back to top |
|
jeegar Smarty n00b
Joined: 27 Dec 2006 Posts: 4 Location: India
|
|
Back to top |
|
skrebbel Smarty n00b
Joined: 03 Jan 2007 Posts: 4 Location: Eindhoven, the Netherlands
|
Posted: Wed Jan 03, 2007 10:48 am Post subject: alternative solution |
|
|
Hi all,
As an alternative solution to the whole array assigning thingy, I've slightly modified the assign compiler function in the current Smarty version. Now it alternatively allows PHP expressions rather than string values to be assigned. It features a tiny hack to circumvent the Smarty _parse_attr's automatically double-quoting attribute values:
Code: |
function smarty_compiler_assign($tag_attrs, &$compiler)
{
$_params = $compiler->_parse_attrs($tag_attrs);
if (!isset($_params['var'])) {
$compiler->_syntax_error("assign: missing 'var' parameter", E_USER_WARNING);
return;
}
if (isset($_params['value'])) {
return "\$this->assign({$_params['var']}, {$_params['value']});";
} elseif (isset($_params['expr'])) {
return "\$this->assign({$_params['var']}, ".$compiler->_dequote($_params['expr']).");";
} else {
$compiler->_syntax_error("assign: missing 'value' or 'expr' parameter", E_USER_WARNING);
return;
}
}
|
and in some template:
Code: |
{assign var="r5" expr="range(1,5)"}
{foreach from="$r5" item="k"}{$k}{/foreach}
|
Note the use of an "expr" attribute instead of a "value" attribute. This allows *any* PHP expression to be assigned, thus including something like
Code: |
{assign var="interests" expr="array('flowers', 'golf', 'yoghurt')"}
|
|
|
Back to top |
|
skrebbel Smarty n00b
Joined: 03 Jan 2007 Posts: 4 Location: Eindhoven, the Netherlands
|
Posted: Wed Jan 03, 2007 10:57 am Post subject: |
|
|
oh, and as a small comment on the level of evilness of this solution: I believe it's just about as evil as {php}, which makes it rather evil, but obviously not evil enough to not be part of Smarty :-)
that said, i believe this solution makes even-more-evil ways obsolete, such as the {assign var="moo" value=","|explode("zonk,boink,plop")} and the {php} $this->assign('moo', array('zonk','boink','plop')) {/php} that have been proposed earlier. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Jan 06, 2007 10:30 am Post subject: |
|
|
It would be even better if it disabled itself under the appropriate security settings, yes? |
|
Back to top |
|
skrebbel Smarty n00b
Joined: 03 Jan 2007 Posts: 4 Location: Eindhoven, the Netherlands
|
Posted: Tue Jan 16, 2007 11:28 am Post subject: |
|
|
ahyes, i forgot about those. i'll fix that some day soon. do you think the idea is otherwise proper for inclusion into the Smarty distribution? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Jan 16, 2007 4:17 pm Post subject: |
|
|
@skrebbel: I already said I think this is a better topic for Smarty 3.x. I doubt it will ever make it into 2.x. |
|
Back to top |
|
roth_heiko Smarty n00b
Joined: 21 Sep 2006 Posts: 4 Location: Germany, Mosbach
|
Posted: Sun Jan 21, 2007 6:57 pm Post subject: topic for Smarty 3 |
|
|
Sorry boots, but Version 3 is in discussion since April 2005. There's no development right now.
And as I said before, we use this feature for one year now in all of our projects with no drawbacks.
Because there was no discussion of side effects or examples of them and there's no security argument against the patch I'm not glad, that the patch isn't included. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Jan 21, 2007 11:34 pm Post subject: Re: topic for Smarty 3 |
|
|
roth_heiko wrote: | Sorry boots, but Version 3 is in discussion since April 2005. There's no development right now.
And as I said before, we use this feature for one year now in all of our projects with no drawbacks.
Because there was no discussion of side effects or examples of them and there's no security argument against the patch I'm not glad, that the patch isn't included. |
Hi roth.
There are a lot of patches I wanted to make that would have helped me out but I couldn't make them because they would change Smarty in ways that would make others unhappy. So, I'm not trying to be the bad guy here, but adding in support for arrays is no small change.
Personally, I think there wasn't a lot of discussion on this topic just because not a lot of interest exists for it. That doesn't mean your patch is not good (sorry, I didn't actually review it) but instead, it means that maybe Smarty users en masse aren't ready or don't need this facility?
I know that direct appeals have been made to all of the core Smarty developers to take this up and I know that not much interest has been shown in that group. It would be nice if monte or messju commented here, but maybe their silence is already their answer?
Best regards. |
|
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
|
|