library/smarty/libs/sysplugins/smarty_internal_compile_section.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Internal Plugin Compile Section
       
     4  * Compiles the {section} {sectionelse} {/section} tags
       
     5  *
       
     6  * @package    Smarty
       
     7  * @subpackage Compiler
       
     8  * @author     Uwe Tews
       
     9  */
       
    10 
       
    11 /**
       
    12  * Smarty Internal Plugin Compile Section Class
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage Compiler
       
    16  */
       
    17 class Smarty_Internal_Compile_Section extends Smarty_Internal_CompileBase {
       
    18     /**
       
    19      * Attribute definition: Overwrites base class.
       
    20      *
       
    21      * @var array
       
    22      * @see Smarty_Internal_CompileBase
       
    23      */
       
    24     public $required_attributes = array('name', 'loop');
       
    25     /**
       
    26      * Attribute definition: Overwrites base class.
       
    27      *
       
    28      * @var array
       
    29      * @see Smarty_Internal_CompileBase
       
    30      */
       
    31     public $shorttag_order = array('name', 'loop');
       
    32     /**
       
    33      * Attribute definition: Overwrites base class.
       
    34      *
       
    35      * @var array
       
    36      * @see Smarty_Internal_CompileBase
       
    37      */
       
    38     public $optional_attributes = array('start', 'step', 'max', 'show');
       
    39 
       
    40     /**
       
    41      * Compiles code for the {section} tag
       
    42      *
       
    43      * @param  array $args array with attributes from parser
       
    44      * @param  object $compiler compiler object
       
    45      *
       
    46      * @return string compiled code
       
    47      */
       
    48     public function compile($args, $compiler) {
       
    49         // check and get attributes
       
    50         $_attr = $this->getAttributes($compiler, $args);
       
    51 
       
    52         $this->openTag($compiler, 'section', array('section', $compiler->nocache));
       
    53         // maybe nocache because of nocache variables
       
    54         $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
       
    55 
       
    56         $output = "<?php ";
       
    57 
       
    58         $section_name = $_attr['name'];
       
    59 
       
    60         $output .= "if (isset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name])) unset(\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]);\n";
       
    61         $section_props = "\$_smarty_tpl->tpl_vars['smarty']->value['section'][$section_name]";
       
    62 
       
    63         foreach ($_attr as $attr_name => $attr_value) {
       
    64             switch ($attr_name) {
       
    65                 case 'loop':
       
    66                     $output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int) \$_loop); unset(\$_loop);\n";
       
    67                     break;
       
    68 
       
    69                 case 'show':
       
    70                     if (is_bool($attr_value)) {
       
    71                         $show_attr_value = $attr_value ? 'true' : 'false';
       
    72                     } else {
       
    73                         $show_attr_value = "(bool) $attr_value";
       
    74                     }
       
    75                     $output .= "{$section_props}['show'] = $show_attr_value;\n";
       
    76                     break;
       
    77 
       
    78                 case 'name':
       
    79                     $output .= "{$section_props}['$attr_name'] = $attr_value;\n";
       
    80                     break;
       
    81 
       
    82                 case 'max':
       
    83                 case 'start':
       
    84                     $output .= "{$section_props}['$attr_name'] = (int) $attr_value;\n";
       
    85                     break;
       
    86 
       
    87                 case 'step':
       
    88                     $output .= "{$section_props}['$attr_name'] = ((int) $attr_value) == 0 ? 1 : (int) $attr_value;\n";
       
    89                     break;
       
    90             }
       
    91         }
       
    92 
       
    93         if (!isset($_attr['show'])) {
       
    94             $output .= "{$section_props}['show'] = true;\n";
       
    95         }
       
    96 
       
    97         if (!isset($_attr['loop'])) {
       
    98             $output .= "{$section_props}['loop'] = 1;\n";
       
    99         }
       
   100 
       
   101         if (!isset($_attr['max'])) {
       
   102             $output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
       
   103         } else {
       
   104             $output .= "if ({$section_props}['max'] < 0)\n" . "    {$section_props}['max'] = {$section_props}['loop'];\n";
       
   105         }
       
   106 
       
   107         if (!isset($_attr['step'])) {
       
   108             $output .= "{$section_props}['step'] = 1;\n";
       
   109         }
       
   110 
       
   111         if (!isset($_attr['start'])) {
       
   112             $output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
       
   113         } else {
       
   114             $output .= "if ({$section_props}['start'] < 0)\n" . "    {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" . "else\n" . "    {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
       
   115         }
       
   116 
       
   117         $output .= "if ({$section_props}['show']) {\n";
       
   118         if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
       
   119             $output .= "    {$section_props}['total'] = {$section_props}['loop'];\n";
       
   120         } else {
       
   121             $output .= "    {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
       
   122         }
       
   123         $output .= "    if ({$section_props}['total'] == 0)\n" . "        {$section_props}['show'] = false;\n" . "} else\n" . "    {$section_props}['total'] = 0;\n";
       
   124 
       
   125         $output .= "if ({$section_props}['show']):\n";
       
   126         $output .= "
       
   127             for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
       
   128                  {$section_props}['iteration'] <= {$section_props}['total'];
       
   129                  {$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
       
   130         $output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
       
   131         $output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
       
   132         $output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
       
   133         $output .= "{$section_props}['first']      = ({$section_props}['iteration'] == 1);\n";
       
   134         $output .= "{$section_props}['last']       = ({$section_props}['iteration'] == {$section_props}['total']);\n";
       
   135 
       
   136         $output .= "?>";
       
   137 
       
   138         return $output;
       
   139     }
       
   140 }
       
   141 
       
   142 /**
       
   143  * Smarty Internal Plugin Compile Sectionelse Class
       
   144  *
       
   145  * @package    Smarty
       
   146  * @subpackage Compiler
       
   147  */
       
   148 class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase {
       
   149     /**
       
   150      * Compiles code for the {sectionelse} tag
       
   151      *
       
   152      * @param  array $args array with attributes from parser
       
   153      * @param  object $compiler compiler object
       
   154      *
       
   155      * @return string compiled code
       
   156      */
       
   157     public function compile($args, $compiler) {
       
   158         // check and get attributes
       
   159         $_attr = $this->getAttributes($compiler, $args);
       
   160 
       
   161         list($openTag, $nocache) = $this->closeTag($compiler, array('section'));
       
   162         $this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache));
       
   163 
       
   164         return "<?php endfor; else: ?>";
       
   165     }
       
   166 }
       
   167 
       
   168 /**
       
   169  * Smarty Internal Plugin Compile Sectionclose Class
       
   170  *
       
   171  * @package    Smarty
       
   172  * @subpackage Compiler
       
   173  */
       
   174 class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase {
       
   175     /**
       
   176      * Compiles code for the {/section} tag
       
   177      *
       
   178      * @param  array $args array with attributes from parser
       
   179      * @param  object $compiler compiler object
       
   180      *
       
   181      * @return string compiled code
       
   182      */
       
   183     public function compile($args, $compiler) {
       
   184         // check and get attributes
       
   185         $_attr = $this->getAttributes($compiler, $args);
       
   186 
       
   187         // must endblock be nocache?
       
   188         if ($compiler->nocache) {
       
   189             $compiler->tag_nocache = true;
       
   190         }
       
   191 
       
   192         list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('section', 'sectionelse'));
       
   193 
       
   194         if ($openTag == 'sectionelse') {
       
   195             return "<?php endif; ?>";
       
   196         } else {
       
   197             return "<?php endfor; endif; ?>";
       
   198         }
       
   199     }
       
   200 }