kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file compiler.set.php

Documentation is available at compiler.set.php

  1. <?php
  2. /**
  3.  * Smarty {set} compiler function plugin
  4.  *
  5.  * File:     compiler.set.php
  6.  * Type:     compiler function
  7.  * Name:     set
  8.  * Purpose:  Set a value to a variable (also arrays).
  9.  * The optional parameter "if" is used to set the value if the test is true. The test can be: 'empty', '!empty', 'is_null', '!is_null', 'isset', '!isset', 'is_void'.
  10.  * The new command 'is_void' test if the variable is empty and != 0, very useful for test $_REQUEST parameters.
  11.  *
  12.  * @link http://smarty.incutio.com/?page=set
  13.  * @link http://www.dav-muz.net/
  14.  * @version 1.0
  15.  * @copyright Copyright 2006 by Muzzarelli Davide
  16.  * @author Davide Muzzarelli <info@dav-muz.net>
  17.  *
  18.  * @package kernel
  19.  * @subpackage plugins
  20.  */
  21.  
  22. /**
  23.  * Set Compiler Function
  24.  * 
  25.  * @param array parameters "var": variable. "value": value to assign. "if": assign the value only if this test is true (tests avaiables: 'empty', '!empty', 'is_null', '!is_null', 'isset', '!isset', 'is_void').
  26.  * @param Smarty_Compiler object
  27.  * @return void|string
  28.  */ 
  29. function smarty_compiler_set($params, &$smarty) {
  30.     // Extract if "value" parameter contain an array
  31.     $regularExpression = '/ value=array\([\'"]?.*[\'"]?\)/';
  32.     if (preg_match($regularExpression, $params, $array)) {
  33.         $array = substr($array[0], 7);
  34.         $params = preg_replace($regularExpression, '', $params);
  35.     }
  36.  
  37.     $params = $smarty->_parse_attrs($params);
  38.     $functionsPermitted = array('empty', '!empty', 'is_null', '!is_null', 'isset', '!isset', 'is_void'); // Functions permitted in "if" parameter.
  39.  
  40.     if (!isset($params['var'])) {
  41.         $smarty->_syntax_error("set: missing 'var' parameter", E_USER_WARNING);
  42.         return;
  43.     }
  44.     if (!empty($array)) {
  45.         $params['value'] = $array;
  46.     }
  47.  
  48.     if (!isset($params['value'])) { // Clean setting
  49.         return "{$params['var']} = null;";
  50.     } elseif (isset($params['if'])) { // Setting with "if" parameter
  51.         $params['if'] = substr($params['if'], 1, -1);
  52.         if (in_array($params['if'], $functionsPermitted)) {
  53.             if ($params['if'] == 'is_void') { // "is_void" command
  54.                 return "if (empty({$params['var']}) and ({$params['var']} !== 0) and ({$params['var']} !== '0')) {$params['var']} = {$params['value']};";
  55.             } else { // others commands
  56.                 return "if ({$params['if']}({$params['var']})) {$params['var']} = {$params['value']};";
  57.             }
  58.         } else { // "if" parameter not correct
  59.             $smarty->_syntax_error("set: 'if' parameter not valid", E_USER_WARNING);
  60.             return;
  61.         }
  62.     } else { // normal setting
  63.         return "{$params['var']} = {$params['value']};";
  64.     }
  65. }
  66. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:15 +0000 by phpDocumentor 1.5.0-lsces