• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Templates in CI version 4

#1
Hey guys

First, I'm not sure if 'template' is the technical term for this. In CI version 3, I created a MY_Loader file in the core folder and I used that to load the Header, View, and Footer with just one line of code:

MY_Loader.php
[Image: XwHIGiA.png]

Then inside the controllers I'd use it like this:

Controller
[Image: EOEmf5v.png]

How do I do this with CI 4? So far I've been loading the views for the Header, View, and Footer files individually in each instance, which is not ideal if I want to switch templates (from v1 to v2, for example).
Reply

#2
You can do it almost the same way.

Add your template method to the CI 4 BaseController.

Then extend all your controllers from the BaseController.

I tested it here and it worked fine.

BaseController:

PHP Code:
    public function template(string $page, array $data)
    {
        echo view('templates/v1/header'$data);
        echo view($page$data);
        echo view('templates/v1/footer'$data);
    

Then call it like this:

PHP Code:
$this->template('auth/login, $data); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Take a look at View Layouts which accomplish much the same thing and it might even be more versatile and easier to use than the extended loader method.
Reply

#4
Thank you @InsiteFX, I initially tried something similar but couldn't get it to work. Your method works great. Also, @dave friend thank you for the suggestion, I will check it out now.
Cheers
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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