Arrays indexed by arrays
| From: | Ludovic LANGE | Date: | Sun, 28 Jul 2002 23:25:46 +0000 |
| Subject: | Arrays indexed by arrays | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-5486@lists.php.net to get a copy of this message | ||
Hello,
(Sorry for the length of the post, but it's not that easy to explain...)
I'm trying to do the following : (in two nested sections i & j )
{$myvar.array[ $myothervar[ j ] . index ] [ i ] }
((*) Why do I want to do this ? See at the end of the post)
Unfortunately, this is not well compiled by Smarty. (it detects it as a 'function' plugin, instead of a var ).
I think it's just a matter of compilation, for the PHP code to be generated, even if it seems rather complicated, can easily be deducted by the following observation of the compilation result :
- Simple Indexed variable : {$myvar.array[i]}
<?php echo $this->_tpl_vars['myvar']['array'][ $this->_sections['i']['index'] ]; ?>
- Double indexation : {$myvar.array[j][i]}
<?php echo $this->_tpl_vars['myvar']['array'][ $this->_sections['j']['index'] ][ $this->_sections['i']['index'] ]; ?>
- Indexed by a variable instead of a section {$myvar.array[$myothervar][i]}
<?php echo $this->_tpl_vars['myvar']['array'][ $this->_tpl_vars['myothervar'] ][ $this->_sections['i']['index'] ]; ?>
So, the compilation result of : {$myvar.array[ $myothervar[ j ] ][ i ]}
should be :
<?php echo $this->_tpl_vars['myvar']['array'][ $this->_tpl_vars['myothervar'][ $this->_sections['j']['index'] ] ][ $this->_sections['i']['index'] ]; ?>
(instead, there is a compilation error and it ouputs something like :
<?php $this->_plugins['function']['$myvar.array[$myothervar[j]][i]'][0](array(), $this); if($this->_extract) { extract($this->_tpl_vars); $this->_extract=false; } ?> )
I think the reason of this failure is in the (rather big and difficult to understand ) regular expression that parses the tags, in Smarty_Compiler.class.php, "_compile_tag".
This piece of expression ( slightly edited : ^\$\w+((\[(\d+|\$\w+|\w+(\.\w+)?)\])|((\.|->)\$?\w+))* to fit in KRegexpEditor ;-)) ) seems to be the main culprit... because it only allows, between the brackets, either a digit, a variable name, or a section attribute. Not another indexed variable.
Of course, this is not easy, because I think this reaches the limits of a regular expression matching (perharps a Lex/Yacc analyzer would be better suited to detect an indexed 'variable' as an index of another 'variable'.)
So, my questions are :
- Is it possible to enhance the regular expression to add this ability ? (of course, modifying the regular expression will only add one level of indexation, and will complicate it)
- Is there another (easier way) of doing the same ? I'm so concentrated on this problem that I'm surely forgetting the obvious way of having the same result.
(I have another solution which falls back on $myvar.array[ j ][ i ], the template is easier to read, but then the PHP code is way more complex... So I'm not that stuck. )
Thank you for your attention !
Regards,
Ludovic LANGE
(*) Why do I need to access such a complex variable ? Ok, let me explain :
I have an HTML form, which is an (i) columns x (j) rows table (i and j are variables, and depend on the execution context).
In each cell, I have a textfield. its name is : myvar[ {$myothervar[j] . index } ][]
This, when submitted, will give in PHP a two-dimensional array ($myvar ), and the first index will be indexed by $myothervar[j].id (which comes from a database as you may expect).
AND (at least !!) there is a default value, in each textfield, which is, you guessed it : {$myvar.array[ $myothervar[ j ] . index ] [ i ] } (Well, it should be.)