Creating a CI service for using templating engine? |
I want to understand what is the best place to create instance of $templates object. I am using Plates templating engine.
Code: // Create new Plates instance Code: class Blog extends \CodeIgniter\Controller I know I could create that Plates instance in index() method but I'd have to repeat in every method OR I could assign to $this e.g. $this->templates and set it in the contructor. It still feels like is not the best method. I have very little experience of Laravel but I have noticed I could simply use their view() method to make use of their Blade templating engine without explicitly defining layout in each controller. I would like to achieve something similar so that I don't have to create $templates instance in each and every controller files. I have read throught the documentation of CI-4 and I have an inkling that it might be achieveable by creating a Service (https://bcit-ci.github.io/CodeIgniter4/c...vices.html ) or pehaps simply using config files? I am not sure. Any ideas how do I achieve that in CI-4? What do you say?
03-22-2018, 03:08 AM
(This post was last modified: 03-22-2018, 03:17 AM by gabpnr. Edit Reason: missing comma )
Hello,
yes, the best way is certainly by creating a service to initliase the Plate class and call his render. Something like this : application/Config/Services.php Code: // ... and in any controller action Code: $data = [ |
Welcome Guest, Not a member yet? Register Sign In |