How to integrate Twig template using Code igniter? How to integrate Twig template using Code igniter? codeigniter codeigniter

How to integrate Twig template using Code igniter?


The best and simplier I know is this:

Start by including twig to your project using composer (which will keep it up to date) :

composer require "twig/twig:^2.0"

Then, create the file application/libraries/Twig.php (capitalization is important) containing:

<?phpdefined('BASEPATH') OR exit('No direct script access allowed');require_once(FCPATH . 'vendor/autoload.php');class Twig {    private $twig;    public function __construct()    {        $loader = new Twig_Loader_Filesystem(APPPATH . 'views');        $this->twig = new Twig_Environment($loader);    }    public function render($template, $placeholders)    {        return $this->twig->render($template . '.php', $placeholders);    }}

Lastly, use it in a controller like this:

public function index(){    $this->load->library('twig'); // Can also be autoloaded    echo $this->twig->render('some_page_in_views', ['foo' => 'barr']);}


Someone has already made the library for linkage between twig and ci.

You can see the link below

https://github.com/bmatschullat/Twig-Codeigniter