Skip to content

Issue with renderSection() on view layout, should be prevented from clearing data after display #7069

Closed
@addngr

Description

@addngr

PHP Version

7.4

CodeIgniter4 Version

4.2.12

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MariaDB 10.2

What happened?

It is not possible to reuse the string result of a View Layout $this->renderSection() due the function clear the data after display.
I have a use case, Where, I need to re-use the section multiple times in View Layout.
E.g.

<!doctype html>
<html>
<head>
    <title><?= $this->renderSection('page_title') ?></title> <!------------- Work Fine ! -->
</head>
<body>
    <h1 class="heading"><?= $this->renderSection('page_title') ?></h1>  <!------------- Problem Here ! -->
    <?= $this->renderSection('content') ?>
</body>
</html>

Steps to Reproduce

Use renderSection() multiple times for same variable in a View Layout.

Expected Output

Expect to can save result of renderSection() and reuse.

/**
    * 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, bool $saveData = false)
{
    if (! isset($this->sections[$sectionName])) {
        echo '';

        return;
    }

    foreach ($this->sections[$sectionName] as $key => $contents) {
        echo $contents;
        if($saveData === false)
        {
             unset($this->sections[$sectionName][$key]);
        }
    }
}

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementPRs that improve existing functionalities

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions