Welcome Guest, Not a member yet? Register   Sign In
Twig integration in CI4
#7
Thumbs Up 

There are several ways to do this.

In my case, I followed Twig's own documentation.

In the baseController I made the following changes:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

use 
Twig\Loader\FilesystemLoader;
use 
Twig\Environment;

class 
BaseController extends Controller
{
  protected $helpers = [];
  protected $twig;
  
  
/**
  * Constructor.
  *
  * @param RequestInterface  $request
  * @param ResponseInterface $response
  * @param LoggerInterface  $logger
  */
  public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
  {    
    $appPaths 
= new \Config\Paths();
    $appViewPaths $appPaths->viewDirectory;

    $loader = new FilesystemLoader($appViewPaths);

    $this->twig = new Environment($loader, [
      'auto_reload' => true,
      'cache' => WRITEPATH.'/cache/twig',
    ]);
  }
  



In the methods I use to display the views, I simply replace the 'echo view() with '$this->twig->render()'


PHP Code:
...

echo 
$this->twig->render('index.html'$data);

.. 

You can create a helper or a function if you prefer and even replace the original view () function.

You can see more examples in the documentation https://twig.symfony.com/

This is what is amazing about this Framework, it offers you several resources, but it does not prevent you from adding other resources.

Congratulations on the incredible work of the team.
Reply


Messages In This Thread
Twig integration in CI4 - by murugappan - 06-06-2020, 06:23 PM
RE: Twig integration in CI4 - by jreklund - 06-07-2020, 01:14 AM
RE: Twig integration in CI4 - by ivantcholakov - 06-07-2020, 04:54 AM
RE: Twig integration in CI4 - by murugappan - 06-07-2020, 03:02 PM
RE: Twig integration in CI4 - by jreklund - 06-08-2020, 10:50 AM
RE: Twig integration in CI4 - by murugappan - 06-09-2020, 06:30 AM
RE: Twig integration in CI4 - by Marcos Queiroz - 05-04-2021, 11:57 AM
RE: Twig integration in CI4 - by daycry - 05-11-2021, 05:41 AM
RE: Twig integration in CI4 - by kai_lf - 11-05-2021, 09:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB