Configure Codeigniter with Twig

In my recent project i tried to integrate codeigniter with twig template engine with php-activerecord and so thought to share with everyone.
First you need to have codeigniter downloaded and extracted then download php-activerecord. Now lets integrate both:
1 – Extract active record and put it inside /libraries folder so the directory structure becomes /application/libraries/php-activerecord.
2 – Create a class Activerecord.php inside libraries folder itself with the following contents

3 – Configure your config/database.php file to match with your database settings.

And that’s it!! You are done… And can use this easily in your application. All you now need to do is , load the library whenever you want to use it. And to get rid of this you can simple put a line in your config/autoload.php

Now lets configure codeigniter with twig engine.
For this first get Twig. After extracting you can place this too in your application/libraries folder so that your directory structure becomes

Afterward, you need to create a file named Twig.php in libraries directory and place following code into that

I would like to take you through the functions defined in this file first. Well, first two functions are to add function to twig which will help you call php/codeigniter functions in your template files.
Function title is to add title to the page and function append is to append title to the title you have defined earlier. For example we can create a base title of the website something like “Google” and then append strings to it to appear something like “Google | About” and in these cases append function is very useful.
Set function is used to set a variable which can be accessed in template files and if you set “global=TRUE”, you can access it in your all template files. These all functions may look trivial but actually these are very useful to separate your logic(Controllers) from view completely as view files are only responsible to display data and it’s controller who should handle all the logic behind those data.
Now finally you need to create a file twig.php in your config directory with following contents:

And that’s it 🙂 You are done with it.
Now you have to create some view files. You can do it at your own also but in my case i created a folder called _layouts inside views(directory you had configured earlier to handle template files) directory and another directory to place my pages. So my directory structure becomes

So first i created a file application.html.twig in my _layouts/ folder like this.

You see these {% block %} thing? The first one is used to display the title you had set in your controller using that “title” and “append” function we discussed earlier. And the second one to display the page content which will come later.
Now create another file index.html.twig in pages directory with the following contents:

So now your template files are ready. You can extend this application.html.twig file from anywhere in your views directory. Now you need to call these files from your controller. It’s simple as following:

Now one last line needs to be added in your autoload.php and that is:

And that’s it.. I hope you enjoyed this.
Please feel free to comment or ask me any query.

3 Responses

    1. I have edited my post. Please see that.
      And if you have 10 templates you can simply extend index.html.twig by simply putting following line on top:

      You can extend it any number of files. It simply follows Object Oriented Paradigm so you can see it that way too.

  1. After following all steps i m getting blank page….upto i m getting data $this->twig->title(‘Dashboard’);………..after that i m getting blank page any suggestion?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top