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:

JavaScript

and my view looks something like this:

JavaScript

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:

JavaScript

and then at my template:

JavaScript

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:

JavaScript

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:

JavaScript

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

JavaScript

So my final method will look like this:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement