From 1ade2e72da6757e8702fac9c461445be3d70330f Mon Sep 17 00:00:00 2001 From: Add-ngr Date: Sun, 15 Jan 2023 21:26:39 +0530 Subject: [PATCH 1/5] feat: `renderSection` option to retained data --- system/View/View.php | 9 +++++++-- tests/system/View/ViewTest.php | 10 ++++++++++ .../system/View/Views/extend_reuse_section.php | 9 +++++++++ tests/system/View/Views/layout_welcome.php | 3 +++ user_guide_src/source/outgoing/view_layouts.rst | 17 +++++++++++++++-- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 tests/system/View/Views/extend_reuse_section.php create mode 100644 tests/system/View/Views/layout_welcome.php diff --git a/system/View/View.php b/system/View/View.php index 9a39104728df..842c9993414b 100644 --- a/system/View/View.php +++ b/system/View/View.php @@ -406,8 +406,11 @@ public function endSection() /** * Renders a section's contents. + * + * @param bool $saveData If true, saves data for subsequent calls, + * if false, cleans the data after displaying. */ - public function renderSection(string $sectionName) + public function renderSection(string $sectionName, bool $saveData = false) { if (! isset($this->sections[$sectionName])) { echo ''; @@ -417,7 +420,9 @@ public function renderSection(string $sectionName) foreach ($this->sections[$sectionName] as $key => $contents) { echo $contents; - unset($this->sections[$sectionName][$key]); + if ($saveData === false) { + unset($this->sections[$sectionName][$key]); + } } } diff --git a/tests/system/View/ViewTest.php b/tests/system/View/ViewTest.php index 537740231f66..11b823c80fed 100644 --- a/tests/system/View/ViewTest.php +++ b/tests/system/View/ViewTest.php @@ -387,4 +387,14 @@ public function testRenderNestedSections() $this->assertStringContainsString('

Second

', $content); $this->assertStringContainsString('

Third

', $content); } + + public function testRenderSectionSavingData() + { + $view = new View($this->config, $this->viewsDir, $this->loader); + $expected = "Welcome to CodeIgniter 4!\n

Welcome to CodeIgniter 4!

\n

Hello World

"; + + $view->setVar('pageTitle', 'Welcome to CodeIgniter 4!'); + $view->setVar('testString', 'Hello World'); + $this->assertSame($expected, $view->render('extend_reuse_section')); + } } diff --git a/tests/system/View/Views/extend_reuse_section.php b/tests/system/View/Views/extend_reuse_section.php new file mode 100644 index 000000000000..f7a5c0a263e2 --- /dev/null +++ b/tests/system/View/Views/extend_reuse_section.php @@ -0,0 +1,9 @@ +extend('layout_welcome') ?> + +section('page_title') ?> + +endSection() ?> + +section('content') ?> + +endSection() ?> diff --git a/tests/system/View/Views/layout_welcome.php b/tests/system/View/Views/layout_welcome.php new file mode 100644 index 000000000000..0eebf376fb7e --- /dev/null +++ b/tests/system/View/Views/layout_welcome.php @@ -0,0 +1,3 @@ +<?= $this->renderSection('page_title', true) ?> +

renderSection('page_title') ?>

+

renderSection('content') ?>

\ No newline at end of file diff --git a/user_guide_src/source/outgoing/view_layouts.rst b/user_guide_src/source/outgoing/view_layouts.rst index 2600b1c08922..daa326c65a01 100644 --- a/user_guide_src/source/outgoing/view_layouts.rst +++ b/user_guide_src/source/outgoing/view_layouts.rst @@ -31,8 +31,21 @@ E.g. **app/Views/default.php**:: -The ``renderSection()`` method only has one argument - the name of the section. That way any child views know -what to name the content section. +The ``renderSection()`` method have two arguments ``$sectionName`` - the name of the section. That way any child views know +what to name the content section. And ``$saveData`` - If true, saves data for subsequent calls, if false, cleans the data after displaying. + +E.g. **app/Views/welcome_message.php**:: + + + + + <?= $this->renderSection('page_title', true) ?> + + +

renderSection('page_title') ?>

+

renderSection('content') ?>

+ + ********************** Using Layouts in Views From fb34e69b54919fd58dd381b43e602050815e167f Mon Sep 17 00:00:00 2001 From: Add-ngr Date: Mon, 16 Jan 2023 23:09:28 +0530 Subject: [PATCH 2/5] changelog v4.4.0 added --- user_guide_src/source/changelogs/v4.4.0.rst | 78 +++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 user_guide_src/source/changelogs/v4.4.0.rst diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst new file mode 100644 index 000000000000..bb7579e4c48d --- /dev/null +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -0,0 +1,78 @@ +Version 4.4.0 +############# + +Release Date: Unreleased + +**4.4.0 release of CodeIgniter4** + +.. contents:: + :local: + :depth: 3 + +Highlights +********** + +- TBD + +BREAKING +******** + +Behavior Changes +================ + +Interface Changes +================= + +Method Signature Changes +======================== + +Enhancements +************ + +Commands +======== + +Testing +======= + +Database +======== + +Query Builder +------------- + +Forge +----- + +Others +------ + +Model +===== + +Libraries +========= + +Helpers and Functions +===================== + +Others +====== +- **View:** Added optional 2nd argument on ``renderSection('page_title', true)`` to prevent from auto cleans the data after displaying. See ([#7126](https://github.com/codeigniter4/CodeIgniter4/pull/7126)) for details. + + +Message Changes +*************** + +Changes +******* + +Deprecations +************ + +Bugs Fixed +********** + +See the repo's +`CHANGELOG.md `_ +for a complete list of bugs fixed. From becb4806dde2aec9447ce8f45d802d5df8570f98 Mon Sep 17 00:00:00 2001 From: Add-ngr Date: Wed, 18 Jan 2023 23:55:56 +0530 Subject: [PATCH 3/5] feat: renderSection option to retained data, after 1st review change --- tests/system/View/ViewTest.php | 2 +- tests/system/View/Views/layout_welcome.php | 2 +- user_guide_src/source/changelogs/index.rst | 1 + user_guide_src/source/changelogs/v4.4.0.rst | 2 +- user_guide_src/source/outgoing/view_layouts.rst | 7 +++++-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/system/View/ViewTest.php b/tests/system/View/ViewTest.php index 11b823c80fed..61742d943984 100644 --- a/tests/system/View/ViewTest.php +++ b/tests/system/View/ViewTest.php @@ -395,6 +395,6 @@ public function testRenderSectionSavingData() $view->setVar('pageTitle', 'Welcome to CodeIgniter 4!'); $view->setVar('testString', 'Hello World'); - $this->assertSame($expected, $view->render('extend_reuse_section')); + $this->assertStringContainsString($expected, $view->render('extend_reuse_section')); } } diff --git a/tests/system/View/Views/layout_welcome.php b/tests/system/View/Views/layout_welcome.php index 0eebf376fb7e..a3623a4aafad 100644 --- a/tests/system/View/Views/layout_welcome.php +++ b/tests/system/View/Views/layout_welcome.php @@ -1,3 +1,3 @@ <?= $this->renderSection('page_title', true) ?>

renderSection('page_title') ?>

-

renderSection('content') ?>

\ No newline at end of file +

renderSection('content') ?>

diff --git a/user_guide_src/source/changelogs/index.rst b/user_guide_src/source/changelogs/index.rst index 9932e8725891..cdffd1cb94f0 100644 --- a/user_guide_src/source/changelogs/index.rst +++ b/user_guide_src/source/changelogs/index.rst @@ -12,6 +12,7 @@ See all the changes. .. toctree:: :titlesonly: + v4.4.0 v4.3.2 v4.3.1 v4.3.0 diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index bb7579e4c48d..0203732d15e5 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -58,7 +58,7 @@ Helpers and Functions Others ====== -- **View:** Added optional 2nd argument on ``renderSection('page_title', true)`` to prevent from auto cleans the data after displaying. See ([#7126](https://github.com/codeigniter4/CodeIgniter4/pull/7126)) for details. +- **View:** Added optional 2nd argument on ``renderSection('page_title', true)`` to prevent from auto cleans the data after displaying. See :ref:`View Layouts ` for details. Message Changes diff --git a/user_guide_src/source/outgoing/view_layouts.rst b/user_guide_src/source/outgoing/view_layouts.rst index daa326c65a01..f6292d4b95c8 100644 --- a/user_guide_src/source/outgoing/view_layouts.rst +++ b/user_guide_src/source/outgoing/view_layouts.rst @@ -31,8 +31,9 @@ E.g. **app/Views/default.php**:: -The ``renderSection()`` method have two arguments ``$sectionName`` - the name of the section. That way any child views know -what to name the content section. And ``$saveData`` - If true, saves data for subsequent calls, if false, cleans the data after displaying. +The ``renderSection()`` method has two arguments: ``$sectionName`` and ``$saveData``. ``$sectionName`` is the name of +the section used by any child view to name the content section. If the boolean argument ``$saveData`` is set to true, +the method saves data for subsequent calls. Otherwise, the method cleans the data after displaying the contents. E.g. **app/Views/welcome_message.php**:: @@ -47,6 +48,8 @@ E.g. **app/Views/welcome_message.php**:: +.. note:: ``$saveData`` can be used since v4.4.0. + ********************** Using Layouts in Views ********************** From 40d649df4377933bb4f39c9aa244a3af4fc411e1 Mon Sep 17 00:00:00 2001 From: Add-ngr Date: Thu, 19 Jan 2023 08:50:11 +0530 Subject: [PATCH 4/5] changelog updated in v4.4.0 --- user_guide_src/source/changelogs/v4.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 0203732d15e5..6632a061c4d1 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -58,7 +58,7 @@ Helpers and Functions Others ====== -- **View:** Added optional 2nd argument on ``renderSection('page_title', true)`` to prevent from auto cleans the data after displaying. See :ref:`View Layouts ` for details. +- **View:** Added optional 2nd parameter ``$saveData`` on ``renderSection()`` to prevent from auto cleans the data after displaying. See :ref:`View Layouts ` for details. Message Changes From 29ebd6736727570b7d6045055872874461d2a386 Mon Sep 17 00:00:00 2001 From: Add-ngr Date: Thu, 19 Jan 2023 20:01:13 +0530 Subject: [PATCH 5/5] defined a label on **Creating A Layout** section --- user_guide_src/source/outgoing/view_layouts.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/user_guide_src/source/outgoing/view_layouts.rst b/user_guide_src/source/outgoing/view_layouts.rst index f6292d4b95c8..6e6c672710fd 100644 --- a/user_guide_src/source/outgoing/view_layouts.rst +++ b/user_guide_src/source/outgoing/view_layouts.rst @@ -12,6 +12,8 @@ any view being rendered. You could create different layouts to support one-colum blog archive pages, and more. Layouts are never directly rendered. Instead, you render a view, which specifies the layout that it wants to extend. +.. _creating-a-layout: + ***************** Creating A Layout *****************