CIX Bundled with twig template engine. And configured with CIX_twig extension to allow some Codeigniter function in your twig template Here some instruction to get best out of your twig template.
- Enable twig feature:
If initially you chose not to use twig, don't worry, you can install it again just by running composer update twig/twig
after enabling the use-twig value in your composer file
- Disable Twig Temporarily:
If you have installed but do not want to use the twig for some reason, you can temporarily disable the twig. To do so just set $config['enable_twig'] = FALSE;
at {APPDIR}/config/config.php.
- Twig template location:
The Twig template is configurable throw a config variable. Set $config['twig_dir'] = 'your_chosen_dir';
at {APPDIR}/config/config.php. By default it is "twig".
- Twig Instance:
You can access the Twig instance from your controller by $this->twig();
- Create Twig Extension
You can create you own extension following the twig guideline. You can put the extension in the application/library directory or in your own library bundle created in the src/libs directory using Name space. Then you can register the extension in two way. You can register as with global instance or you can register on demand. To register with global twig instance you need to do following things:
- Add it to the configuration file.
$config['twig_extensions'] = array('your_extension_with_full_name_space');
at {APPDIR}/config/config.php - define constructor in your extension with a parameter that expect CI_Controller Object (optional, but this way you will get access to the controller object from your extension)
public function __construct(\CI_Controller $ci)
{
}
You can also register twig extension whenever you need by using the Twig instance.
$this->twig()->addExtension(new Your_Twig_Extension());
- CIX Twig Global Variables:
There are some global variables available to use in the twig templates:
- APPPATH - The path to the Application directory
- DIRECTORY_SEPARATOR - The OS depended directory separator (\ or /)
- __FILE__ - Current twig template path
- controller - The current controller path
- app.user - Current logged in user
- app.session - Current user session
- CIX Twig functions:
You can call any global function from twig template just by call the function prefixed with php_. The implementation is inspired from LidaaTwigBundle. For an example, if you like to use site_url() function you can call it as php_site_url()
Besides that here is the available list of CIX twig functions you can use. More function will be added soon....
- _t - localization translating function (will only work if your application is localize-ready)
- nonce - Create a nonce variable
- valid_nonce - validate nonce value
- logout_url - return logout url
- Assetic Bundle.
Cix packed with Assetic Bundle for smart assets management. Run following command to dump assets files.
> php bin/cli assetic:dump
*For full Twig reference, visit Twig Site