Created: 2011-07-14 04:39
Updated: 2014-05-05 01:54


This Spark integrates Twig and CI2.

Twig is a template language for PHP, released under the new BSD license (code and documentation). Twig uses a syntax similar to the Django and Jinja template languages which inspired the Twig runtime environment.

Installation and configuration

After installing this Spark, you have to put the Twig library in the SPARK_DOCTRINE2_PATH/vendors directory of your application and check if the path_twig_lib configuration key in SPARK_DOCTRINE2_PATH/config/twig.php matches with your filesystem. In case you directly clone the whole Twig project in your vendors dir (by typing git clone, the default path should do.

Once twig has been copied, you should be able to use this Spark like any others, by loading it in your controller:


and then by invoking the $this->twig->dsp() method, provided for your convenience, as follows:

$this->twig->dsp('my-view-file.html', array(
	  'myVar1' => array(
	  array('href' => '', 'caption' => 'Code Igniter'),
	  array('href' => '', 'caption' => 'Sparks'),
	'myVar2' => 'fooBarBaz',

In case you need finer controls over Twig properties, two method are provided in order to access them, getEnv() and getLoader().

Known bugs and roadmap

There's a bug with Sparks autoloading in CI 2.0.2, therefore you still have to load the Twig Spark manually. That should be an issue about to be solved.

At the moment this Spark contains a drop-in solution for start using Twig as template engine instead of CI's default one. A better integration with CI's Loader and view layer would be desirable.

Resources and further readings:

Contacts and footnotes

This Spark has been sponsored by Agavee Team and written by Claudio Beatrice (twitter: @omissis)

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more