[ Index ]

PHP Cross Reference of Xoops v2.3.1

[Global whois Lookup]    [Ranchi, Jharkhand, India website]     [Hindi Magazine]     [Desi Community website in tristate area]    [B 4 Bollywood]    [Internet nation of India]

title

Body

[close]

/class/smarty/xoops_plugins/ -> compiler.foreachq.php (source)

   1  <?php
   2  /**

   3   * foreachq Smarty compiler plug-in

   4   *

   5   * See the enclosed file LICENSE for licensing information.

   6   * If you did not receive this file, get it at http://www.fsf.org/copyleft/gpl.html

   7   *

   8   * @copyright   The XOOPS project http://www.xoops.org/

   9   * @license     http://www.fsf.org/copyleft/gpl.html GNU public license

  10   * @author        Skalpa Keo <skalpa@xoops.org>

  11   * @package        xos_opal

  12   * @subpackage    xos_opal_Smarty

  13   * @since       2.0.14

  14   * @version        $Id: compiler.foreachq.php 506 2006-05-26 23:10:37Z skalpa $

  15   */
  16  
  17  /**

  18   * Quick foreach template plug-in

  19   *

  20   * This plug-in works as a direct replacement for the original Smarty

  21   * {@link http://smarty.php.net/manual/en/language.function.foreach.php foreach} function.

  22   * 

  23   * The difference with <var>foreach</var> is minimal in terms of functionality, but can boost your templates

  24   * a lot: foreach duplicates the content of the variable that is iterated, to ensure non-array

  25   * variables can be specified freely. This implementation does not do that, but as a consequence

  26   * requires that the variable you specify in the <var>from</var> parameter is an array or

  27   * (when using PHP5) an object. Check the difference between the code generated by foreach

  28   * and foreachq to understand completely.

  29   * 

  30   * <b>Note:</b> to use foreachq, only the opening tag has to be replaced. The closing tab still

  31   * remains {/foreach}

  32   * 

  33   * <code>

  34   * // Iterate, slow version

  35   * {foreach from=$array item=elt}

  36   *   {$elt}

  37   * {/foreach}

  38   * // Iterate, fast version

  39   * {foreachq from=$array item=elt}

  40   *   {$elt}

  41   * {/foreach}

  42   * </code>

  43   */
  44  function smarty_compiler_foreachq( $argStr, &$comp ) {
  45  
  46      $comp->_push_tag('foreach');
  47  
  48      $attrs = $comp->_parse_attrs( $argStr, false );
  49  
  50      $arg_list = array();
  51  
  52      if (empty($attrs['from'])) {
  53          return $comp->_syntax_error("foreachq: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
  54      }
  55      $from = $attrs['from'];
  56  
  57      if (empty($attrs['item'])) {
  58          return $comp->_syntax_error("foreachq: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
  59      }
  60      $item = $comp->_dequote($attrs['item']);
  61      if (!preg_match('~^\w+$~', $item)) {
  62          return $comp->_syntax_error("'foreachq: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  63      }
  64  
  65      if (isset($attrs['key'])) {
  66          $key  = $comp->_dequote($attrs['key']);
  67          if (!preg_match('~^\w+$~', $key)) {
  68              return $comp->_syntax_error("foreachq: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
  69          }
  70          $key_part = "\$this->_tpl_vars['$key'] => ";
  71      } else {
  72          $key = null;
  73          $key_part = '';
  74      }
  75  
  76      if (isset($attrs['name'])) {
  77          $name = $attrs['name'];
  78      } else {
  79          $name = null;
  80      }
  81  
  82      $output = '';
  83      //$output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";

  84      if (isset($name)) {
  85          $foreach_props = "\$this->_foreach[$name]";
  86          $output .= "{$foreach_props} = array('total' => count($from), 'iteration' => 0);\n";
  87          //$output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";

  88          $output .= "if ({$foreach_props}['total'] > 0):\n";
  89          $output .= "    foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
  90          //$output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";

  91          $output .= "        {$foreach_props}['iteration']++;\n";
  92      } else {
  93          $output .= "if (count($from)):\n";
  94          $output .= "    foreach ($from as $key_part\$this->_tpl_vars['$item']):\n";
  95          //$output .= "if (count(\$_from)):\n";

  96          //$output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";

  97      }
  98      //$output .= '';

  99  
 100      return $output;
 101  
 102  }
 103  
 104  ?>


[ Xoops]     [PhpNuke]     [PostNuke]     [Joomla]    [Drupal]    [E107]    [NucleusCms]
[Php-Fusion]     [PhpBB]     [WordPress]     [Typo3]
Generated: Mon Oct 27 11:51:45 2008
Open Source related documentation for developers.