Skip to content

Commit

Permalink
Fixing issue where elements did not get .ctp as a fallback extension …
Browse files Browse the repository at this point in the history
…unlike view and layout files.

Adding a protected method to get extensions, as it would be in 3 places now.
Added tests.
Fixes #1438
  • Loading branch information
markstory committed Jan 13, 2011
1 parent f893e3b commit 0f4c905
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
39 changes: 25 additions & 14 deletions cake/libs/view/view.php
Expand Up @@ -372,11 +372,13 @@ function element($name, $params = array(), $loadHelpers = false) {
}
}
$paths = $this->_paths($plugin);

foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $this->ext)) {
$file = $path . 'elements' . DS . $name . $this->ext;
break;
$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {
if (file_exists($path . 'elements' . DS . $name . $ext)) {
$file = $path . 'elements' . DS . $name . $ext;
break;
}
}
}

Expand Down Expand Up @@ -862,10 +864,7 @@ function _getViewFileName($name = null) {
}
$paths = $this->_paths(Inflector::underscore($this->plugin));

$exts = array($this->ext);
if ($this->ext !== '.ctp') {
array_push($exts, '.ctp');
}
$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {
if (file_exists($path . $name . $ext)) {
Expand Down Expand Up @@ -905,11 +904,8 @@ function _getLayoutFileName($name = null) {
}
$paths = $this->_paths(Inflector::underscore($this->plugin));
$file = 'layouts' . DS . $subDir . $name;

$exts = array($this->ext);
if ($this->ext !== '.ctp') {
array_push($exts, '.ctp');
}

$exts = $this->_getExtensions();
foreach ($exts as $ext) {
foreach ($paths as $path) {
if (file_exists($path . $file . $ext)) {
Expand All @@ -920,6 +916,21 @@ function _getLayoutFileName($name = null) {
return $this->_missingView($paths[0] . $file . $this->ext, 'missingLayout');
}


/**
* Get the extensions that view files can use.
*
* @return array Array of extensions view files use.
* @access protected
*/
function _getExtensions() {
$exts = array($this->ext);
if ($this->ext !== '.ctp') {
array_push($exts, '.ctp');
}
return $exts;
}

/**
* Return a misssing view error message
*
Expand Down
15 changes: 15 additions & 0 deletions cake/tests/cases/libs/view/view.test.php
Expand Up @@ -573,6 +573,21 @@ function testElementCache() {

}

/**
* test that ctp is used as a fallback file extension for elements
*
* @return void
*/
function testElementCtpFallback() {
$View = new TestView($this->PostsController);
$View->ext = '.missing';
$element = 'test_element';
$expected = 'this is the test element';
$result = $View->element($element);

$this->assertEqual($expected, $result);
}

/**
* testLoadHelpers method
*
Expand Down

0 comments on commit 0f4c905

Please sign in to comment.