This package provides simple Twig integration for CodeIgniter 3.x.
codeigniter/
└── application/
└── libraries/
└── Twig.php
- PHP 5.4.0 or later
- Twig 1.22.0 or later (Also, simply checked with Twig v2.x)
- Add code below to composer.json
"require": {
...
"kenjis/codeigniter-ss-twig": "dev-master"
},
"repositories": [
...
{
"type": "git",
"url": "https://github.com/tdhungit/codeigniter-ss-twig.git"
}
]
Install libraries/Twig.php
to your CodeIgniter application folder:
$ php vendor/kenjis/codeigniter-ss-twig/install.php
- Above command always overwrites exisiting files.
- You must run it at CodeIgniter project root folder.
Add composer.json file
{
"require": {
"kenjis/codeigniter-ss-twig": "dev-master"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/tdhungit/codeigniter-ss-twig.git"
}
]
}
Install libraries/Twig.php
to your CodeIgniter application folder:
$ php vendor/kenjis/codeigniter-ss-twig/install.php
Uncomment this line
require_once APPPATH . '../vendor/autoload.php';
$this->load->library('twig');
You can override the default configuration:
$config = [
'paths' => ['/path/to/twig/templates', VIEWPATH],
'cache' => '/path/to/twig/cache',
];
$this->load->library('twig', $config);
Render Twig template and output to browser:
$this->twig->display('welcome', $data);
Above code renders views/welcome.twig
.
Note: I've changed the method name from
render()
todisplay()
. Nowrender()
method returns string only.
Render Twig template:
$output = $this->twig->render('welcome', $data);
Above code renders views/welcome.twig
.
$this->twig->addGlobal('sitename', 'My Awesome Site');
$twig = $this->twig->getTwig();
base_url
site_url
anchor
form_open
form_close
form_error
form_hidden
set_value
Some helpers are added the functionality of auto-escaping for security.
You can add your functions with configuration:
$config = [
'functions' => ['my_helper'],
'functions_safe' => ['my_safe_helper'],
];
$this->load->library('twig', $config);
If your function explicitly outputs HTML code, you will want the raw output to be printed. In such a case, use functions_safe
, and you have to make sure the output of the function is XSS free.