Skip to content
Advertisement

How to use renderSection in Codeigniter 4?

From the official documentation of Codeigniter 4 for view layouts there is a function renderSection that you can use at your templates.

The main problem though is that I couldn’t figure out how this works. Please have in mind that I don’t want a work-around for this, I really need to know how the renderSection works and what I am missing.

To be more precise:

At my Controller I have this code:

// TestController.php
...
// My method
...

$view = ConfigServices::renderer();

$view->setVar('output', $output);

return $view->render('my-main-view');
...

and my view looks something like this:

// my-main-view.php
...
<div class="main-container">
    <main>
        <?php echo $output; ?>
    </main>
    <?php echo $this->renderSection('sidebar'); ?>
</div>
...

So my question is:

Can I somehow add the data that I would like to have for the ‘sidebar’ from my controller? I would like to use the renderSection specifically to understand how it works.

Please do not add a work-around for this. I know that I could just have the:

$view->setVar('sidebar', view('my-view'));

and then at my template:

echo $sidebar;

The main reason that I am asking this is that we usually find work-arounds and we are missing the point of how to actually use the renderSection

Is there any kind of:

$view->setRenderSection('sidebar', 'my-view', ['data' => $data]);

that I can use? Am I missing something here?

Advertisement

Answer

After spending some time to understand how the section is working, I could finally find a solution to the render section.

The solution is not quite obvious thought. It seems that there isn’t any setRenderSection so I did create my own to my controller. More specifically to the previous example we had:

$view = ConfigServices::renderer();

$view->setVar('output', $output);

return $view->render('my-main-view');

So I’ve added a function at my Controller with name _setRenderSection :

private function _setRenderSection($viewRenderer, $sectionName , $viewName, $data = []) {
    $viewRenderer->section($sectionName);
    echo view($viewName, $data);
    $viewRenderer->endSection($sectionName);
}

So my final method will look like this:

$view = ConfigServices::renderer();

$view->setVar('output', $output);

$this->_setRenderSection($view, 'sidebar' ,'my-view', ['data' => '123']);

return $view->render('my-main-view');

I am also copying the full Controller just in case you would like to copy-paste it:

<?php namespace AppControllers;

class Webpages extends BaseController
{

    private function _setRenderSection($viewRenderer, $sectionName , $viewName, $data = []) {
        $viewRenderer->section($sectionName);
        echo view($viewName, $data);
        $viewRenderer->endSection($sectionName);
    }

    public function about()
    {
        $view = ConfigServices::renderer();

        $view->setVar('output', 'Hello World!');

        $this->_setRenderSection($view, 'sidebar' ,'my-view', ['data' => '123']);

        return $view->render('my-main-view');
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement