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
RE: Twig integration in CI4 - by kenjis - 03-20-2023, 01:10 AM
RE: Twig integration in CI4 - by foxbille - 04-07-2023, 09:22 AM
RE: Twig integration in CI4 - by filipussetio - 04-27-2023, 09:22 AM
RE: Twig integration in CI4 - by foxbille - 08-23-2023, 07:44 AM
RE: Twig integration in CI4 - by murugappan - 03-20-2023, 01:48 AM
RE: Twig integration in CI4 - by kenjis - 04-07-2023, 06:37 PM
RE: Twig integration in CI4 - by foxbille - 04-08-2023, 11:56 AM
RE: Twig integration in CI4 - by kenjis - 04-08-2023, 04:59 PM
RE: Twig integration in CI4 - by foxbille - 04-12-2023, 02:05 AM
RE: Twig integration in CI4 - by kenjis - 04-12-2023, 05:26 AM
RE: Twig integration in CI4 - by foxbille - 04-14-2023, 02:43 AM
RE: Twig integration in CI4 - by kenjis - 04-14-2023, 03:17 AM
RE: Twig integration in CI4 - by kenjis - 04-14-2023, 03:26 AM
RE: Twig integration in CI4 - by foxbille - 04-17-2023, 04:55 AM
RE: Twig integration in CI4 - by ozornick - 08-23-2023, 07:55 AM
RE: Twig integration in CI4 - by foxbille - 08-23-2023, 09:45 AM
RE: Twig integration in CI4 - by ozornick - 08-23-2023, 09:56 AM
RE: Twig integration in CI4 - by foxbille - 08-24-2023, 07:13 AM
RE: Twig integration in CI4 - by kenjis - 08-24-2023, 05:05 PM
RE: Twig integration in CI4 - by foxbille - 09-02-2023, 09:37 AM
RE: Twig integration in CI4 - by ALTITUDE_DEV - 12-29-2023, 04:31 AM



Theme © iAndrew 2016 - Forum software by © MyBB