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

During the installation of CI4 using Composer, i was happy to see that i could use Composer to install Twig by running " composer require kint-php/kint-twig". I did this and Twig got installed.

Is that enough? Will the CI4 autoload the Twig library?

Thank you in advance.
Reply
#2

Yes, it will find it like any other Composer packages you can install.
Reply
#3

No, it is not enough. There should be a bridge to CI4's API, but I don't know whether such thing has been developed yet. If you call Twig directly for example, you would not be able to use CI4's features like locating templates, caching, viewcells.
Reply
#4

(This post was last modified: 06-07-2020, 03:03 PM by murugappan.)

I managed to install the Twig component using Composer without any problem. The problem is that Twig is not integrated. I could integrated Twig easily with CI3 but honestly dont know how to do it CI4. It is so sad that the documentation for CI4 does not cover the installation of its extensions.

Appreciate any help. Thank you.
Reply
#5

(This post was last modified: 06-08-2020, 10:58 AM by jreklund.)

@ivantcholakov: If you want that tight of connection, you need to develop that yourself. I don't know of any CI4 Twig Parser library. You can take a look on how our View Parser where built an incorporate twig instead.

@murugappan: You can only use it natively as there are no CI4 package to extend it. If you are using Compopser, you can access those libraries directly ( In this case \Twig ). You don't need to include an files. Composer Support

Please see Twig template for CI4 for more information about the matter.
Reply
#6

(This post was last modified: 06-09-2020, 10:01 AM by jreklund.)

Hi guys,

I found the integration library for twig in the following link https://github.com/deathart/BlogCI4/blob...g/Twig.php
Awesome. Will test it out. Thank you all.

Stay Safe, Stay Free, Stay United.
Reply
#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
#8

Hi,

This is my integration vendor for twig.

https://github.com/daycry/twig
Reply
#9

Hey daycry,
nice work!
I have installed it and everything works fine :-)
Do you know how to integrate the "u" filter (https://twig.symfony.com/doc/3.x/filters/u.html) which is a twig extension.
I downloaded ist with composer (composer require twig/string-extra) - but it will not be recognized.

Any help will be appreciated.

THX, Kai
Reply
#10

it really help, thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB