diff --git a/lib/Cake/Console/Command/Task/TemplateTask.php b/lib/Cake/Console/Command/Task/TemplateTask.php index 32196243b63..e9f5de95a09 100644 --- a/lib/Cake/Console/Command/Task/TemplateTask.php +++ b/lib/Cake/Console/Command/Task/TemplateTask.php @@ -20,6 +20,7 @@ use Cake\Console\Shell; use Cake\Core\App; use Cake\Utility\Folder; +use Cake\Utility\ViewVarsTrait; /** * Template Task can generate templated output Used in other Tasks. @@ -29,12 +30,7 @@ */ class TemplateTask extends Shell { -/** - * variables to add to template scope - * - * @var array - */ - public $templateVars = array(); + use ViewVarsTrait; /** * Paths to look for templates on. @@ -105,31 +101,6 @@ protected function _findThemes() { return $themes; } -/** - * Set variable values to the template scope - * - * @param string|array $one A string or an array of data. - * @param string|array $two Value in case $one is a string (which then works as the key). - * Unused if $one is an associative array, otherwise serves as the values to $one's keys. - * @return void - */ - public function set($one, $two = null) { - if (is_array($one)) { - if (is_array($two)) { - $data = array_combine($one, $two); - } else { - $data = $one; - } - } else { - $data = array($one => $two); - } - - if (!$data) { - return false; - } - $this->templateVars = $data + $this->templateVars; - } - /** * Runs the template * @@ -148,7 +119,7 @@ public function generate($directory, $filename, $vars = null) { $themePath = $this->getThemePath(); $templateFile = $this->_findTemplate($themePath, $directory, $filename); if ($templateFile) { - extract($this->templateVars); + extract($this->viewVars); ob_start(); ob_implicit_flush(0); include $templateFile; diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index 5162994daaa..aebc3230d89 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -33,6 +33,7 @@ use Cake\Routing\Router; use Cake\Utility\Inflector; use Cake\Utility\ObjectCollection; +use Cake\Utility\ViewVarsTrait; /** * View, the V in the MVC triad. View interacts with Helpers and view variables passed @@ -66,6 +67,7 @@ class View extends Object { use RequestActionTrait; + use ViewVarsTrait; /** * Helpers collection @@ -797,32 +799,6 @@ public function uuid($object, $url) { return $hash; } -/** - * Allows a template or element to set a variable that will be available in - * a layout or other element. Analogous to Controller::set(). - * - * @param string|array $one A string or an array of data. - * @param string|array $two Value in case $one is a string (which then works as the key). - * Unused if $one is an associative array, otherwise serves as the values to $one's keys. - * @return void - */ - public function set($one, $two = null) { - $data = null; - if (is_array($one)) { - if (is_array($two)) { - $data = array_combine($one, $two); - } else { - $data = $one; - } - } else { - $data = array($one => $two); - } - if (!$data) { - return false; - } - $this->viewVars = $data + $this->viewVars; - } - /** * Magic accessor for helpers. *