associative arrays as parameters
| From: | Heiko Roth | Date: | Tue, 05 Sep 2006 13:00:11 +0000 |
| Subject: | associative arrays as parameters | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-2891@lists.php.net to get a copy of this message | ||
Hello Monte,
I send you a small patch which allows associative arrays as plugin params.
An example:
{navigation id=$site->rootId var=nav
query.fields="id,name,url,order_field"
query.where="type='news/entry'"}
Plugin 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:
{navigation id=$site->rootId var=nav
query="array('fields'=>'id,name,url,order_field',
'where'=>'type=\'news/entry\''}
Plugin 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
{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:
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.
Heiko Roth.
Dipl. Inform.
Geschäftsführer
EGOTEC GmbH
Alte Neckarelzer Straße 24 D
D-74821 Mosbach
Telefon +49 (0) 6261/674318
Fax +49 (0) 6261/674329
E-Mail roth@egotec.com
Internet www.egocms.com
PGP-ID: 0x5EA92BC7