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..61742d943984 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->assertStringContainsString($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..a3623a4aafad --- /dev/null +++ b/tests/system/View/Views/layout_welcome.php @@ -0,0 +1,3 @@ +<?= $this->renderSection('page_title', true) ?> +

renderSection('page_title') ?>

+

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 new file mode 100644 index 000000000000..6632a061c4d1 --- /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 parameter ``$saveData`` on ``renderSection()`` to prevent from auto cleans the data after displaying. See :ref:`View Layouts ` for details. + + +Message Changes +*************** + +Changes +******* + +Deprecations +************ + +Bugs Fixed +********** + +See the repo's +`CHANGELOG.md `_ +for a complete list of bugs fixed. diff --git a/user_guide_src/source/outgoing/view_layouts.rst b/user_guide_src/source/outgoing/view_layouts.rst index 2600b1c08922..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 ***************** @@ -31,8 +33,24 @@ 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 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**:: + + + + + <?= $this->renderSection('page_title', true) ?> + + +

renderSection('page_title') ?>

+

renderSection('content') ?>

+ + + +.. note:: ``$saveData`` can be used since v4.4.0. ********************** Using Layouts in Views