root/branches/release-26/php/lib/block.mtfor.php @ 1174

Revision 1174, 1.8 kB (checked in by bchoate, 23 months ago)

Updated copyright year for source.

Line 
1<?php
2# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
3# This program is distributed under the terms of the
4# GNU General Public License, version 2.
5#
6# $Id$
7
8function smarty_block_mtfor($args, $content, &$ctx, &$repeat) {
9    $localvars = array('__for_end', '__for_var');
10
11    if (!isset($content)) {
12        $ctx->localize($localvars);
13        // first invocation; setup loop
14        $start = array_key_exists('start', $args) ?
15            $args['start']
16            : (array_key_exists('from', $args) ?
17               $args['from']
18               : 0);
19        $end = array_key_exists('end', $args) ? $args['end']
20            : (array_key_exists('to', $args) ? $args['to'] : null);
21        $var = $args['var'];
22
23        if ($end === null) {
24            $content = '';
25            $repeat = false;
26        }
27
28        $index = $start;
29        $counter = 1;
30        $ctx->stash('__for_end', $end);
31        $ctx->stash('__for_var', $var);
32    } else {
33        $index = $ctx->__stash['vars']['__index__'] + 1;
34        $counter = $ctx->__stash['vars']['__counter__'] + 1;
35        $end = $ctx->stash('__for_end');
36        $var = $ctx->stash('__for_var');
37    }
38
39    if ($index <= $end) {
40        $ctx->__stash['vars']['__index__'] = $index;
41        $ctx->__stash['vars']['__counter__'] = $counter;
42        $ctx->__stash['vars']['__odd__'] = ($counter % 2) == 1;
43        $ctx->__stash['vars']['__even__'] = ($counter % 2) == 0;
44        $ctx->__stash['vars']['__first__'] = $counter == 1;
45        $ctx->__stash['vars']['__last__'] = $index == $end;
46        if ($var)
47            $ctx->__stash['vars'][$var] = $index;
48        if (array_key_exists('glue', $args)) {
49            if ($index < $end)
50                $content = $content . $args['glue'];
51        }
52        $repeat = true;
53    } else {
54        $ctx->restore($localvars);
55        $repeat = false;
56    }
57
58    return $content;
59}
60?>
Note: See TracBrowser for help on using the browser.