![]() |
Creating a CI service for using templating engine? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Creating a CI service for using templating engine? (/showthread.php?tid=70297) |
Creating a CI service for using templating engine? - happyape - 03-22-2018 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/concepts/services.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? RE: Creating a CI service for using templating engine? - gabpnr - 03-22-2018 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 = [ RE: Creating a CI service for using templating engine? - happyape - 03-22-2018 @gabpnr - Thanks very much. Let me give it a go. |