3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
13 * Helper for declaring default values of template variables
15 class DeclareVars
extends AbstractHelper
18 * The view object that created this helper object.
20 * @var \Zend\View\View
25 * Declare template vars to set default values and avoid notices when using strictVars
27 * Primarily for use when using {@link Zend\View\Variables::setStrictVars()},
28 * this helper can be used to declare template variables that may or may
29 * not already be set in the view object, as well as to set default values.
30 * Arrays passed as arguments to the method will be used to set default
31 * values; otherwise, if the variable does not exist, it is set to an empty
39 * array('varName3' => 'defaultValue',
40 * 'varName4' => array()
45 * @param string|array variable number of arguments, all string names of variables to test
48 public function __invoke()
50 $view = $this->getView();
51 $args = func_get_args();
52 foreach ($args as $key) {
54 foreach ($key as $name => $value) {
55 $this->declareVar($name, $value);
57 } elseif (!isset($view->vars()->$key)) {
58 $this->declareVar($key);
66 * Checks to see if a $key is set in the view object; if not, sets it to $value.
69 * @param string $value Defaults to an empty string
72 protected function declareVar($key, $value = '')
74 $view = $this->getView();
75 $vars = $view->vars();
76 if (!isset($vars->$key)) {