Skip to content

Commit

Permalink
Also allow foreach $children[20:] do limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
kolber committed Mar 17, 2011
1 parent 267d1dc commit fa50aaa
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions app/parsers/template-parser.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ static function parse_foreach($data, $template) {
# run the replacements on the pre-"foreach" part of the partial
$template = self::parse($data, $template_parts[1]);
# allow loop limiting
if(preg_match('/\[\d*:\d+\]$/', $template_parts[2])) {
preg_match('/([\$\@].+?)\[(\d*):(\d+)\]$/', $template_parts[2], $matches);
if(preg_match('/\[\d*:\d*\]$/', $template_parts[2])) {
preg_match('/([\$\@].+?)\[(\d*):(\d*)\]$/', $template_parts[2], $matches);
$template_parts[2] = $matches[1];
$start_limit = $matches[2] ? $matches[2] : 0;
$end_limit = $matches[3];
$start_limit = empty($matches[2]) ? 0 : $matches[2];
if (!empty($matches[3])) $end_limit = $matches[3];
}
# traverse one level deeper into the data hierachy
$pages = (isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]])) ? $data[$template_parts[2]] : false;

# slice down the data array if required
if(is_array($pages) && $end_limit) {
if(is_array($pages) && $start_limit !== false) {
$pages = array_slice($pages, $start_limit, $end_limit);
}

Expand Down

1 comment on commit fa50aaa

@benschwarz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One begins to wonder when you should just reserve these items to be done directly in PHP, rather than proxying through regex and writing your own form of pseudo language.

Please sign in to comment.