|
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 |
garamir Smarty n00b
Joined: 14 Aug 2003 Posts: 2
|
Posted: Thu Aug 14, 2003 5:27 pm Post subject: A nested loops question |
|
|
I've read through most of the recent topics on nested loops that a forum search turned up, but most of them seem to boil down to using foreach correctly. However, Smarty chokes on the compiled template, and I'm having trouble pinpointing my error.
I'm working on a page that displays available working hours for an employee, sorted by day. To do this, I create an array of days, then fill each day with an array of start/finish values. Here's a sample of the array that's passed into Smarty:
Code: | Array
(
[Monday] => Array
(
[0] => Array
(
[start] => 2:00 PM
[finish] => 7:00 PM
)
)
[Tuesday] => Array
(
[0] => Array
(
[start] => 11:00 AM
[finish] => 7:00 PM
)
)
[and so on for each day in their schedule]
) |
My example only shows one element per day, but the employee might have more than one work period per day. I attempt to display this with a two-level foreach, like so:
Code: | {foreach from=$work_hours item=work_day key=$key}
{$key}
{foreach from=$work_day item=hour_pair}
{$hour_pair.start} - {$hour_pair.finish}
{/foreach}
{/foreach} |
However, running this, Smarty chokes with an error like:
Code: | [error] PHP Parse error: parse error, expecting `']'' in /home/httpd/smarty/rescenter/templates_c/%%123/%%1234119526/rcapps.tpl.php on line 80 | ....and checking the compiled template, I see it trying to generate something like this:[php:1:b6c1121b55] foreach ((array)$this->_tpl_vars['work_hours'] as $this->_tpl_vars['$this->_tpl_vars['key']'] => $this->_tpl_vars['work_day']):[/php:1:b6c1121b55]
I've also tried using a section:
Code: | {section name=work_day loop=$work_hours}
{section name=hour_pair loop=$work_hours[work_day]}
{$work_hours[work_day][hour_pair].start} - {$work_hours[work_day][hour_pair].finish}
{/section}
{/section} |
...but it gives similar results, with a different line number. I've also tried using dotted notation instead of the square brackets, in case it prefers that notation, but it makes no difference.
Can anyone point out what boneheaded error I'm making? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Aug 14, 2003 5:31 pm Post subject: Re: A nested loops question |
|
|
garamir wrote: |
...
Code: | {foreach from=$work_hours item=work_day key=$key}
|
... |
it should read "key=key" not "key=$key". it's a variable name expected here, not a variable.
HTH
messju |
|
Back to top |
|
garamir Smarty n00b
Joined: 14 Aug 2003 Posts: 2
|
Posted: Thu Aug 14, 2003 7:19 pm Post subject: Re: A nested loops question |
|
|
Why, so it is. Thanks! |
|
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
|