Welcome Guest, Not a member yet? Register   Sign In
Clarification how to use view_cell, layouts or views
#1
Question 

Hi everybody,

I've been struggling on how or when to use which type of view. I understand the differences and I think I know how to use them but it is unclear to me what the benefits are.

Let's say I have the structure as follows:
-------------------
|    Navbar      |
-------------------
|    Main         |
-------------------
|    Footer       |
-------------------

What is the difference between using those structures?

1: 3 php files
echo view(navbar.php, $data1)
echo view(main.php, $data2)
echo view(footer.php, $data1)

2: 1 main.php + view_cells
main.php:
[...]
<body>
    <?= view_cell('App\Libraries\ViewComponents::getNavbar', '$data1') ?>
    <?= view_cell('App\Libraries\ViewComponents::getMain', '$data2') ?>
    <?= view_cell('App\Libraries\ViewComponents::getFooter', '$data1') ?>
</body>

3: Sections
main.php
[...]
<body>
    <?= this->renderSection('page_body') ?>
    <?= this->renderSection('page_body') ?>
    <?= this->renderSection('page_footer') ?>
</body>
Reply
#2

(This post was last modified: 04-15-2020, 04:27 AM by Leo.)

Hi, I'm not sure of the "correct" way either - I do it like this:

In the controller: echo view('front', $this->data);

In view file 'front.php':
<!DOCTYPE html>
<html>
<head><!-- head stuff --></head>
<body>
<?= $this->include('home') ?> <!-- this points to view file home.php in the app/Views directory
<footer><!-- footer stuff --></footer>
</body>
</html>

view file home.php contains:
<main>
<header><h1>Very neat, and semantically correct HTML stuff</h1></header>
<section>....</section>
<!-- I also may have a little, hidden form embeded in this file, that is located in app/Views/modules/popform.php, like so: -->
<?= $this->include('modules/popform') ?>
</main>

If someone more senior sees this and cringes please tell me before I write a lot of stuff this way. Or is it good?
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#3

(This post was last modified: 04-15-2020, 05:33 AM by stlake2011.)

(04-12-2020, 10:42 PM)elserra Wrote: Hi everybody,

I've been struggling on how or when to use which type of view. I understand the differences and I think I know how to use them but it is unclear to me what the benefits are.

Let's say I have the structure as follows:
-------------------
|    Navbar      |
-------------------
|    Main         |
-------------------
|    Footer       |
-------------------

What is the difference between using those structures?

1: 3 php files
echo view(navbar.php, $data1)
echo view(main.php, $data2)
echo view(footer.php, $data1)

2: 1 main.php + view_cells
main.php:
[...]
<body>
    <?= view_cell('App\Libraries\ViewComponents::getNavbar', '$data1') ?>
    <?= view_cell('App\Libraries\ViewComponents::getMain', '$data2') ?>
    <?= view_cell('App\Libraries\ViewComponents::getFooter', '$data1') ?>
</body>

3: Sections
main.php
[...]
<body>
    <?= this->renderSection('page_body') ?>
    <?= this->renderSection('page_body') ?>
    <?= this->renderSection('page_footer') ?>
</body>

Not completely sure myself but I always use the first way you listed as that matches the way I was doing it long before frameworks became a thing. I also find it allows me to have granular control if needed and not having to process/type as much.
Reply
#4

(04-15-2020, 04:23 AM)Leo Wrote: Hi, I'm not sure of the "correct" way either - I do it like this:

[...]
<!-- I also may have a little, hidden form embeded in this file, that is located in app/Views/modules/popform.php, like so: -->
<?= $this->include('modules/popform') ?>
</main>

If someone more senior sees this and cringes please tell me before I write a lot of stuff this way. Or is it good?

Right, that one was missing. What is actually the difference between  $this->include() and echo view() ?
Reply
#5

All three ways work. It's whatever works best for your workflow.

View Cells are a little "heavier" as they have to parse and fire up another class to do the work. I typically think of those as best used for things that have to hit the database and might be on several pages, like a Recent Posts list on a blog, or something like that. Especially since you can easily cache those views so you don't have to hit the database at all.

The only real benefit to using $this->renderSection() and the others is to allow for templates that can be shared among all of your page views for different page layouts (like homepage, two-column, three-column, admin, etc).
Reply
#6

Okokokok I think I get it now. I am developing a blog, that's why I thank you a lo for the heads up with the view cells and caching (although there's not much to put into cache, but for the sake of optimization).
Reply
#7

(This post was last modified: 04-16-2023, 01:46 AM by Corsari.)

(04-15-2020, 01:32 PM)kilishan Wrote: All three ways work. .... t. Especially since you can easily cache those views so you don't have to hit the database at all.

The only real benefit to using $this->renderSection() and the others is to allow for templates that can be shared among all of your page views for different page layouts (like homepage, two-column, three-column, admin, etc).

Hello
cache sounds great

can you kindly hint how to enable the cacheing of such a view cell?
Or it will got cached automatically?

Also: Navigation bar has much sense in a layout template or as cell to be called from the main template file? For readability could be, but is there something to consider about such approach? Pro / Cons

Thank you
Reply
#8

READ:

CodeIgniter 4 User Guide - View Cells - Cell Caching
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

Thank you InsiteFX
One kind hint..
From the link you kindly provided
"You can cache the results of the view cell call by passing the number of seconds to cache the data for as the third parameter. This will use the currently configured cache engine."
Configured Cache Engine sentence, mean?
Thank you
Reply
#10

See https://codeigniter.com/user_guide/libra...-the-cache
Reply




Theme © iAndrew 2016 - Forum software by © MyBB