Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand All @@ -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]);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/system/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,14 @@ public function testRenderNestedSections()
$this->assertStringContainsString('<p>Second</p>', $content);
$this->assertStringContainsString('<p>Third</p>', $content);
}

public function testRenderSectionSavingData()
{
$view = new View($this->config, $this->viewsDir, $this->loader);
$expected = "<title>Welcome to CodeIgniter 4!</title>\n<h1>Welcome to CodeIgniter 4!</h1>\n<p>Hello World</p>";

$view->setVar('pageTitle', 'Welcome to CodeIgniter 4!');
$view->setVar('testString', 'Hello World');
$this->assertStringContainsString($expected, $view->render('extend_reuse_section'));
}
}
9 changes: 9 additions & 0 deletions tests/system/View/Views/extend_reuse_section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?= $this->extend('layout_welcome') ?>

<?= $this->section('page_title') ?>
<?= $pageTitle ?>
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<?= $testString ?>
<?= $this->endSection() ?>
3 changes: 3 additions & 0 deletions tests/system/View/Views/layout_welcome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<title><?= $this->renderSection('page_title', true) ?></title>
<h1><?= $this->renderSection('page_title') ?></h1>
<p><?= $this->renderSection('content') ?></p>
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See all the changes.
.. toctree::
:titlesonly:

v4.4.0
v4.3.2
v4.3.1
v4.3.0
Expand Down
78 changes: 78 additions & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
@@ -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 <creating-a-layout>` for details.


Message Changes
***************

Changes
*******

Deprecations
************

Bugs Fixed
**********

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
22 changes: 20 additions & 2 deletions user_guide_src/source/outgoing/view_layouts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
*****************
Expand All @@ -31,8 +33,24 @@ E.g. **app/Views/default.php**::
</body>
</html>

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**::

<!doctype html>
<html>
<head>
<title><?= $this->renderSection('page_title', true) ?></title>
</head>
<body>
<h1><?= $this->renderSection('page_title') ?><h1>
<p><?= $this->renderSection('content') ?></p>
</body>
</html>

.. note:: ``$saveData`` can be used since v4.4.0.

**********************
Using Layouts in Views
Expand Down