web/lib/Zend/View/Helper/DeclareVars.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @version    $Id: DeclareVars.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** Zend_View_Helper_Abstract.php */
       
    24 require_once 'Zend/View/Helper/Abstract.php';
       
    25 
       
    26 /**
       
    27  * Helper for declaring default values of template variables
       
    28  *
       
    29  * @package    Zend_View
       
    30  * @subpackage Helper
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_View_Helper_DeclareVars extends Zend_View_Helper_Abstract
       
    35 {
       
    36     /**
       
    37      * The view object that created this helper object.
       
    38      * @var Zend_View
       
    39      */
       
    40     public $view;
       
    41 
       
    42     /**
       
    43      * Declare template vars to set default values and avoid notices when using strictVars
       
    44      *
       
    45      * Primarily for use when using {@link Zend_View_Abstract::strictVars() Zend_View strictVars()},
       
    46      * this helper can be used to declare template variables that may or may
       
    47      * not already be set in the view object, as well as to set default values.
       
    48      * Arrays passed as arguments to the method will be used to set default
       
    49      * values; otherwise, if the variable does not exist, it is set to an empty
       
    50      * string.
       
    51      *
       
    52      * Usage:
       
    53      * <code>
       
    54      * $this->declareVars(
       
    55      *     'varName1',
       
    56      *     'varName2',
       
    57      *     array('varName3' => 'defaultValue',
       
    58      *           'varName4' => array()
       
    59      *     )
       
    60      * );
       
    61      * </code>
       
    62      *
       
    63      * @param string|array variable number of arguments, all string names of variables to test
       
    64      * @return void
       
    65      */
       
    66     public function declareVars()
       
    67     {
       
    68         $args = func_get_args();
       
    69         foreach($args as $key) {
       
    70             if (is_array($key)) {
       
    71                 foreach ($key as $name => $value) {
       
    72                     $this->_declareVar($name, $value);
       
    73                 }
       
    74             } else if (!isset($view->$key)) {
       
    75                 $this->_declareVar($key);
       
    76             }
       
    77         }
       
    78     }
       
    79 
       
    80     /**
       
    81      * Set a view variable
       
    82      *
       
    83      * Checks to see if a $key is set in the view object; if not, sets it to $value.
       
    84      *
       
    85      * @param  string $key
       
    86      * @param  string $value Defaults to an empty string
       
    87      * @return void
       
    88      */
       
    89     protected function _declareVar($key, $value = '')
       
    90     {
       
    91         if (!isset($this->view->$key)) {
       
    92             $this->view->$key = $value;
       
    93         }
       
    94     }
       
    95 }