Skip to content

Commit

Permalink
Separate default templates and runtime templates.
Browse files Browse the repository at this point in the history
This makes for fewer workarounds with managing templates vs. having
duplicate conditions for handling string/array templates.

Refs #3142
  • Loading branch information
markstory committed Mar 27, 2014
1 parent fcf0da4 commit ad62e77
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/View/Helper/FormHelper.php
Expand Up @@ -76,7 +76,7 @@ class FormHelper extends Helper {
],
'widgets' => [],
'registry' => null,
'templates' => [
'defaultTemplates' => [
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
Expand Down Expand Up @@ -168,9 +168,8 @@ class FormHelper extends Helper {
* @param \Cake\View\View $View The View this helper is being attached to.
* @param array $config Configuration settings for the helper.
*/
public function __construct(View $View, $config = array()) {
public function __construct(View $View, $config = []) {
parent::__construct($View, $config);

$config = $this->config();

$this->widgetRegistry($config['registry'], $config['widgets']);
Expand Down Expand Up @@ -2238,7 +2237,7 @@ public function widget($name, array $data = []) {
* @return void
*/
public function resetTemplates() {
$this->templates($this->_defaultConfig['templates']);
$this->templates($this->_defaultConfig['defaultTemplates']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/PaginatorHelper.php
Expand Up @@ -49,7 +49,7 @@ class PaginatorHelper extends Helper {
*/
protected $_defaultConfig = [
'options' => [],
'templates' => [
'defaultTemplates' => [
'nextActive' => '<li class="next"><a rel="next" href="{{url}}">{{text}}</a></li>',
'nextDisabled' => '<li class="next disabled"><span>{{text}}</span></li>',
'prevActive' => '<li class="prev"><a rel="prev" href="{{url}}">{{text}}</a></li>',
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/SessionHelper.php
Expand Up @@ -36,7 +36,7 @@ class SessionHelper extends Helper {
* @var array
*/
protected $_defaultConfig = [
'templates' => [
'defaultTemplates' => [
'flash' => '<div id="{{key}}-message" class="{{class}}">{{message}}</div>'
]
];
Expand Down
4 changes: 3 additions & 1 deletion src/View/Helper/StringTemplateTrait.php
Expand Up @@ -66,8 +66,10 @@ public function templater() {
$class = $this->config('templateClass') ?: '\Cake\View\StringTemplate';
$this->_templater = new $class;

$templates = $this->config('templates');
$defaults = $this->config('defaultTemplates');
$this->_templater->add($defaults);

$templates = $this->config('templates');
if ($templates) {
if (is_string($templates)) {
$this->_templater->load($templates);
Expand Down
16 changes: 14 additions & 2 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -143,7 +143,6 @@ public function setUp() {
$this->View = new View(null);

$this->Form = new FormHelper($this->View);
$this->Form->Html = new HtmlHelper($this->View);
$this->Form->request = new Request('articles/add');
$this->Form->request->here = '/articles/add';
$this->Form->request['controller'] = 'articles';
Expand Down Expand Up @@ -187,10 +186,23 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
unset($this->Form, $this->Controller, $this->View);
TableRegistry::clear();
}

/**
* Test construct() with the templates option.
*
* @return void
*/
public function testConstructTemplatesFile() {
$helper = new FormHelper($this->View, [
'templates' => 'htmlhelper_tags.php'
]);
$result = $helper->input('name');
$this->assertContains('<input', $result);
}

/**
* Test registering a new widget class and rendering it.
*
Expand Down

0 comments on commit ad62e77

Please sign in to comment.