Skip to content

Commit

Permalink
Implement ViewVarsTrait in other classes.
Browse files Browse the repository at this point in the history
Both TemplateTask and View has the same copy + paste code.
  • Loading branch information
markstory committed Feb 22, 2013
1 parent 16664ca commit 41cd758
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 58 deletions.
35 changes: 3 additions & 32 deletions lib/Cake/Console/Command/Task/TemplateTask.php
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
Expand Down
28 changes: 2 additions & 26 deletions lib/Cake/View/View.php
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,7 @@
class View extends Object {

use RequestActionTrait;
use ViewVarsTrait;

/**
* Helpers collection
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 41cd758

Please sign in to comment.