Skip to content

Commit

Permalink
Adding view files for tests.
Browse files Browse the repository at this point in the history
Adding test case for elements + extending
Making elements extend each other.
  • Loading branch information
markstory committed Dec 18, 2011
1 parent b6919a0 commit e06895e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/Cake/Test/Case/View/ViewTest.php
Expand Up @@ -1039,6 +1039,23 @@ public function testExtendNested() {
This is the first parent.
This is the first template.
Sidebar Content.
TEXT;
$this->assertEquals($expected, $content);
}

/**
* Test extend() in an element and a view.
*
* @return void
*/
public function testExtendElement() {
$this->View->layout = false;
$content = $this->View->render('extend_element');
$expected = <<<TEXT
Parent View.
View content.
Parent Element.
Element content.
TEXT;
$this->assertEquals($expected, $content);
Expand Down
2 changes: 2 additions & 0 deletions lib/Cake/Test/test_app/View/Elements/extended_element.ctp
@@ -0,0 +1,2 @@
<?php $this->extend('parent_element'); ?>
Element content.
2 changes: 2 additions & 0 deletions lib/Cake/Test/test_app/View/Elements/parent_element.ctp
@@ -0,0 +1,2 @@
Parent Element.
<?php echo $this->fetch('content'); ?>
3 changes: 3 additions & 0 deletions lib/Cake/Test/test_app/View/Posts/extend_element.ctp
@@ -0,0 +1,3 @@
<?php $this->extend('parent_view'); ?>
View content.
<?php echo $this->element('extended_element'); ?>
5 changes: 5 additions & 0 deletions lib/Cake/Test/test_app/View/Posts/nested_extends.ctp
@@ -0,0 +1,5 @@
<?php
$this->extend('parent_1');
$this->assign('sidebar', 'Sidebar Content.');
?>
This is the first template.
5 changes: 5 additions & 0 deletions lib/Cake/Test/test_app/View/Posts/parent_1.ctp
@@ -0,0 +1,5 @@
<?php
$this->extend('parent_2');
?>
This is the first parent.
<?php echo $this->fetch('content'); ?>
3 changes: 3 additions & 0 deletions lib/Cake/Test/test_app/View/Posts/parent_2.ctp
@@ -0,0 +1,3 @@
This is the second parent.
<?php echo $this->fetch('content'); ?>
<?php echo $this->fetch('sidebar'); ?>
2 changes: 2 additions & 0 deletions lib/Cake/Test/test_app/View/Posts/parent_view.ctp
@@ -0,0 +1,2 @@
Parent View.
<?php echo $this->fetch('content') ?>
19 changes: 18 additions & 1 deletion lib/Cake/View/View.php
Expand Up @@ -260,6 +260,14 @@ class View extends Object {
*/
protected $_current = null;

/**
* Currently rendering an element. Used for finding parent fragments
* for elements.
*
* @var boolean
*/
protected $_inElement = false;

/**
* Content stack, used for nested templates that all use View::extend();
*
Expand Down Expand Up @@ -349,7 +357,11 @@ public function element($name, $data = array(), $options = array()) {
if ($callbacks) {
$this->Helpers->trigger('beforeRender', array($file));
}

$this->_inElement = true;
$element = $this->_render($file, array_merge($this->viewVars, $data));
$this->_inElement = false;

if ($callbacks) {
$this->Helpers->trigger('afterRender', array($file, $element));
}
Expand Down Expand Up @@ -640,7 +652,12 @@ public function end() {
* @param string $name The view or element to 'extend' the current one with.
*/
public function extend($name) {
$this->_parents[$this->_current] = $this->_getViewFileName($name);
if ($this->_inElement) {
$parent = $this->_getElementFileName($name);
} else {
$parent = $this->_getViewFileName($name);
}
$this->_parents[$this->_current] = $parent;
}

/**
Expand Down

0 comments on commit e06895e

Please sign in to comment.