• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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>
It is easier to search the internet than posting a question. So, yes, I am asking because I did not find the answer anywhere else.
Reply

#2
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
(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() ?
It is easier to search the internet than posting a question. So, yes, I am asking because I did not find the answer anywhere else.
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).
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.