Skip to content

Commit

Permalink
Adding StringTemplateTrait so that the string template functionality …
Browse files Browse the repository at this point in the history
…can be easy shared between helpers in preparation of refactoring the Form helper to use string templates as well.
  • Loading branch information
Florian Krämer committed Dec 20, 2013
1 parent 6f012c8 commit 6e8752e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
17 changes: 3 additions & 14 deletions Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -16,7 +16,6 @@

use Cake\Utility\Inflector;
use Cake\View\Helper;
use Cake\View\StringTemplate;
use Cake\View\View;

/**
Expand All @@ -28,6 +27,8 @@
*/
class PaginatorHelper extends Helper {

use StringTemplateTrait;

/**
* Holds the default options for pagination links
*
Expand All @@ -44,13 +45,6 @@ class PaginatorHelper extends Helper {
*/
public $options = [];

/**
* StringTemplate instance.
*
* @var Cake\View\StringTemplate
*/
protected $_templater;

/**
* The default templates used by PaginatorHelper.
*
Expand Down Expand Up @@ -83,12 +77,7 @@ class PaginatorHelper extends Helper {
*/
public function __construct(View $View, $settings = []) {
parent::__construct($View, $settings);

$this->_templater = new StringTemplate();
$this->_templater->add($this->_defaultTemplates);
if (isset($settings['templates'])) {
$this->_templater->load($settings['templates']);
}
$this->_initStringTemplates($this->_defaultTemplates);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions Cake/View/Helper/StringTemplateTrait.php
@@ -0,0 +1,56 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Helper;

use Cake\View\StringTemplate;

trait StringTemplateTrait {

/**
* StringTemplate instance.
*
* @var Cake\View\StringTemplate
*/
protected $_templater;

/**
* Initializes the StringTemplate class and loads templates
*
* @param array $templates
* @return void
*/
protected function _initStringTemplates($templates = []) {
$this->_templater = new StringTemplate();
$this->_templater->add($templates);
if (isset($this->settings['templates'])) {
$this->_templater->load($this->settings['templates']);
}
}

/**
* Get/set templates to use.
*
* @param string|null|array $templates null or string allow reading templates. An array
* allows templates to be added.
* @return void|string|array
*/
public function templates($templates = null) {
if ($templates === null || is_string($templates)) {
return $this->_templater->get($templates);
}
return $this->_templater->add($templates);
}

}

0 comments on commit 6e8752e

Please sign in to comment.