nongbit / codeigniter-twig
Integrate Twig to CodeIgniter 4
1.0.0
2024-01-05 12:18 UTC
Requires
- codeigniter4/framework: ^4.0
- twig/intl-extra: ^3.7
- twig/twig: ^3.7
README
Integrate Twig to CodeIgniter 4.
Setup
Download package using composer.
composer require nongbit/codeigniter-twig
Open APPPATH/Controllers/BaseController.php
.
use Nongbit\Twig\Traits\Twig; abstract class BaseController extends Controller { use Twig; ... public function initController(...) { ... $this->initTwig(); } }
Usage
Template
To display template, we can use display()
provided by the trait.
$this->display('hello', ['title' => 'Acme'])
By default, Twig will look for template files inside APPPATH/Views
. To add other locations use addPath()
.
$this->twig->addPath(ROOTPATH . 'templates');
The default file extension is html
but we can change it by creating a configuration file App\Config\Twig.php
.
namespace Config; use CodeIgniter\Config\BaseConfig; class Twig extends BaseConfig { public string $fileExtension = 'twig'; }
Global
$this->twig->addGlobals('title', 'Acme'); $this->twig->addGlobals(['title' => 'Acme']);
Inside view:
{{ title }}
Filter
$this->twig->addFilters('rot13', 'rot13'); $this->twig->addFilters('rot13', function($string) { return str_rot13($string); }); $this->twig->addFitlers(['rot13']);
Inside view :
{{ 'Twig'|rot13 }}
Function
Function behaves similarly to a filter.