Skip to content

Commit

Permalink
Fix template files not being usable with FormHelper::create()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 11, 2014
1 parent dbe809b commit b07ed68
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
15 changes: 9 additions & 6 deletions src/View/Helper/FormHelper.php
Expand Up @@ -293,9 +293,12 @@ public function create($model = null, $options = []) {
];

$this->_idPrefix = $options['idPrefix'];
$templater = $this->getTemplater();

if (!empty($options['templates'])) {
$this->templates($options['templates']);
if (!empty($options['templates']) && is_array($options['templates'])) {
$templater->add($options['templates']);
} elseif (!empty($options['templates']) && is_string($options['templates'])) {
$templater->load($options['templates']);
}
unset($options['templates']);

Expand Down Expand Up @@ -337,11 +340,11 @@ public function create($model = null, $options = []) {
}

if (!empty($append)) {
$append = $this->formatTemplate('hiddenblock', ['content' => $append]);
$append = $templater->format('hiddenblock', ['content' => $append]);
}
$actionAttr = $this->_templater->formatAttributes(['action' => $action, 'escape' => false]);
return $this->formatTemplate('formstart', [
'attrs' => $this->_templater->formatAttributes($htmlAttributes) . $actionAttr
$actionAttr = $templater->formatAttributes(['action' => $action, 'escape' => false]);
return $templater->format('formstart', [
'attrs' => $templater->formatAttributes($htmlAttributes) . $actionAttr
]) . $append;
}

Expand Down
7 changes: 1 addition & 6 deletions src/View/StringTemplate.php
Expand Up @@ -70,12 +70,7 @@ public function __construct(array $templates = null) {
* @return void
*/
public function load($file) {
list($plugin, $file) = pluginSplit($file);
$path = APP . 'Config/';
if ($plugin !== null) {
$path = Plugin::path($plugin) . 'Config/';
}
$loader = new PhpConfig($path);
$loader = new PhpConfig(APP . 'Config/');
$templates = $loader->read($file);
$this->add($templates);
}
Expand Down
20 changes: 19 additions & 1 deletion tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -342,7 +342,7 @@ public function testCreateGet() {
*
* @return void
*/
public function testCreateTemplates() {
public function testCreateTemplatesArray() {
$result = $this->Form->create($this->article, [
'templates' => [
'formstart' => '<form class="form-horizontal"{{attrs}}>',
Expand All @@ -359,6 +359,24 @@ public function testCreateTemplates() {
$this->assertTags($result, $expected);
}

/**
* Test create() with the templates option.
*
* @return void
*/
public function testCreateTemplatesFile() {
$result = $this->Form->create($this->article, [
'templates' => 'htmlhelper_tags.php',
]);
$expected = [
'start form',
'div' => ['class' => 'hidden'],
'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
'/div'
];
$this->assertTags($result, $expected);
}

/**
* test the create() method
*
Expand Down
12 changes: 5 additions & 7 deletions tests/test_app/TestApp/Config/htmlhelper_tags.php
@@ -1,9 +1,7 @@
<?php

$config = array(
'tags' => array(
'form' => 'start form',
'formend' => 'finish form',
'hiddenblock' => '<div class="hidden">%s</div>'
)
);
$config = [
'formstart' => 'start form',
'formend' => 'finish form',
'hiddenblock' => '<div class="hidden">{{content}}</div>'
];

0 comments on commit b07ed68

Please sign in to comment.