|
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 |
rzseattle Smarty Rookie
Joined: 09 Dec 2003 Posts: 7 Location: Poland
|
Posted: Fri Jan 07, 2005 2:47 am Post subject: assing as new array index form template |
|
|
Hello
In my wrork with smarty often happens that I need to create array of indexes in template
expample:
Code: | {callAction path="project/return_category" assign=category }
{foreach from=$category name=y item=cat}
{callAction path="project/return_subcategory" centrum=$cat.id assign=subcategory}
{foreach from=$subcategory name=z item=sub}
{assign var=val[] value=$sub.id}
{/foreach}
{/foreach}
Pass to some selectbox handler or somthing :)
{select name="name" namespace="0" output=$val values=$val selected=$arrow.input.sub }
|
I can't do this directly from template now :/ . Sometimes it's very tiring. I bet that it is not only my problem.
Proposition:
Change assign compiler function to:
[php:1:429206523a]<?php
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'])) {
$compiler->_syntax_error("assign: missing 'value' parameter", E_USER_WARNING);
return;
}
//matches "foo[]"
if( substr( $_params['var'] ,-3, -1 ) == "[]" ){
$varName = substr( $_params['var'] , 1, -3 );
return "
\$compilerAssignVarValue = \$this->get_template_vars( \"$varName\" );
if( !\$compilerAssignVarValue ){
\$compilerAssignVarValue = array();
}
if( !is_array( \$compilerAssignVarValue ) ){
\$this->trigger_error(\"assign: variable '$varName' isn't array\", E_USER_WARNING);
}
\$this->assign( \"$varName\", array_merge( \$compilerAssignVarValue, array ( {$_params['value']} ) ) );";
}else{
return "\$this->assign({$_params['var']}, {$_params['value']});";
}
}
?>[/php:1:429206523a]
What are you think about this? |
|
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
|