Browse files

Adding view files for tests.

Adding test case for elements + extending
Making elements extend each other.
  • Loading branch information...
1 parent b6919a0 commit e06895ef91788207f8be444c8bcd84da67d8e34b @markstory markstory committed Sep 26, 2011
View
17 lib/Cake/Test/Case/View/ViewTest.php
@@ -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);
View
2 lib/Cake/Test/test_app/View/Elements/extended_element.ctp
@@ -0,0 +1,2 @@
+<?php $this->extend('parent_element'); ?>
+Element content.
View
2 lib/Cake/Test/test_app/View/Elements/parent_element.ctp
@@ -0,0 +1,2 @@
+Parent Element.
+<?php echo $this->fetch('content'); ?>
View
3 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'); ?>
View
5 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.
View
5 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'); ?>
View
3 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'); ?>
View
2 lib/Cake/Test/test_app/View/Posts/parent_view.ctp
@@ -0,0 +1,2 @@
+Parent View.
+<?php echo $this->fetch('content') ?>
View
19 lib/Cake/View/View.php
@@ -261,6 +261,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();
*
* @var array
@@ -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));
}
@@ -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;
}
/**

0 comments on commit e06895e

Please sign in to comment.