A library that will help you to create templates for your CodeIgniter 4 application.
- Copy the
app/Libraries/Template.phpfile to yourapp/Librariesfolder. - Copy the
app/Views/template.phpfile to yourapp/Viewsfolder.- Create a folder named
homein yourapp/Viewsfolder. - Create a folder named
layoutin yourapp/Viewsfolder.- Now create 2 files named
header.phpandfooter.phpin yourapp/Views/layoutfolder.
- Now create 2 files named
- Create a folder named
- Load the library in your controller:
use App\Libraries\Template;before the controller class. - Now you can use the library in your controller like this:
Template::Home("view_name")Template::Home("view_name", $data)Template::Home("view_name", $data, 'template_folder_name')this will be the folder where you havelayoutTemplate::YouCustomFunction("view_name", $data)
For the sack of example I have create a Admin function in the Template.php file. You can create your own functions and use them in your controller.
I have already created a folder in the view with the name of admin_side, and I use that folder in the Admin function.
In Home controller you can see I covered all the examples.
PS: You're free to use this library in your project, if you have any questions you can open an issue, also you can contribute to this library.