From e06895ef91788207f8be444c8bcd84da67d8e34b Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 25 Sep 2011 21:52:11 -0400 Subject: [PATCH] Adding view files for tests. Adding test case for elements + extending Making elements extend each other. --- lib/Cake/Test/Case/View/ViewTest.php | 17 +++++++++++++++++ .../View/Elements/extended_element.ctp | 2 ++ .../test_app/View/Elements/parent_element.ctp | 2 ++ .../test_app/View/Posts/extend_element.ctp | 3 +++ .../test_app/View/Posts/nested_extends.ctp | 5 +++++ .../Test/test_app/View/Posts/parent_1.ctp | 5 +++++ .../Test/test_app/View/Posts/parent_2.ctp | 3 +++ .../Test/test_app/View/Posts/parent_view.ctp | 2 ++ lib/Cake/View/View.php | 19 ++++++++++++++++++- 9 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/Cake/Test/test_app/View/Elements/extended_element.ctp create mode 100644 lib/Cake/Test/test_app/View/Elements/parent_element.ctp create mode 100644 lib/Cake/Test/test_app/View/Posts/extend_element.ctp create mode 100644 lib/Cake/Test/test_app/View/Posts/nested_extends.ctp create mode 100644 lib/Cake/Test/test_app/View/Posts/parent_1.ctp create mode 100644 lib/Cake/Test/test_app/View/Posts/parent_2.ctp create mode 100644 lib/Cake/Test/test_app/View/Posts/parent_view.ctp diff --git a/lib/Cake/Test/Case/View/ViewTest.php b/lib/Cake/Test/Case/View/ViewTest.php index 93984c047..f85b7135b 100644 --- a/lib/Cake/Test/Case/View/ViewTest.php +++ b/lib/Cake/Test/Case/View/ViewTest.php @@ -1039,6 +1039,23 @@ This is the second parent. 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 = <<assertEquals($expected, $content); diff --git a/lib/Cake/Test/test_app/View/Elements/extended_element.ctp b/lib/Cake/Test/test_app/View/Elements/extended_element.ctp new file mode 100644 index 000000000..6dd4142d2 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Elements/extended_element.ctp @@ -0,0 +1,2 @@ +extend('parent_element'); ?> +Element content. diff --git a/lib/Cake/Test/test_app/View/Elements/parent_element.ctp b/lib/Cake/Test/test_app/View/Elements/parent_element.ctp new file mode 100644 index 000000000..86e68f64b --- /dev/null +++ b/lib/Cake/Test/test_app/View/Elements/parent_element.ctp @@ -0,0 +1,2 @@ +Parent Element. +fetch('content'); ?> diff --git a/lib/Cake/Test/test_app/View/Posts/extend_element.ctp b/lib/Cake/Test/test_app/View/Posts/extend_element.ctp new file mode 100644 index 000000000..a76c961b7 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Posts/extend_element.ctp @@ -0,0 +1,3 @@ +extend('parent_view'); ?> +View content. +element('extended_element'); ?> diff --git a/lib/Cake/Test/test_app/View/Posts/nested_extends.ctp b/lib/Cake/Test/test_app/View/Posts/nested_extends.ctp new file mode 100644 index 000000000..d0321064a --- /dev/null +++ b/lib/Cake/Test/test_app/View/Posts/nested_extends.ctp @@ -0,0 +1,5 @@ +extend('parent_1'); +$this->assign('sidebar', 'Sidebar Content.'); +?> +This is the first template. diff --git a/lib/Cake/Test/test_app/View/Posts/parent_1.ctp b/lib/Cake/Test/test_app/View/Posts/parent_1.ctp new file mode 100644 index 000000000..e41dfa54b --- /dev/null +++ b/lib/Cake/Test/test_app/View/Posts/parent_1.ctp @@ -0,0 +1,5 @@ +extend('parent_2'); +?> +This is the first parent. +fetch('content'); ?> diff --git a/lib/Cake/Test/test_app/View/Posts/parent_2.ctp b/lib/Cake/Test/test_app/View/Posts/parent_2.ctp new file mode 100644 index 000000000..aedb0f2f1 --- /dev/null +++ b/lib/Cake/Test/test_app/View/Posts/parent_2.ctp @@ -0,0 +1,3 @@ +This is the second parent. +fetch('content'); ?> +fetch('sidebar'); ?> diff --git a/lib/Cake/Test/test_app/View/Posts/parent_view.ctp b/lib/Cake/Test/test_app/View/Posts/parent_view.ctp new file mode 100644 index 000000000..e338b784f --- /dev/null +++ b/lib/Cake/Test/test_app/View/Posts/parent_view.ctp @@ -0,0 +1,2 @@ +Parent View. +fetch('content') ?> diff --git a/lib/Cake/View/View.php b/lib/Cake/View/View.php index eda417b7d..1f4441d83 100644 --- a/lib/Cake/View/View.php +++ b/lib/Cake/View/View.php @@ -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(); * @@ -349,7 +357,11 @@ class View extends Object { 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 @@ class View extends Object { * @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; } /**