opencart/opencart — pull request #1193 by colindecarlo (2014-02-08 02:55:02+00:00)
Title: Add Support the Twig Template Engine

This change will allow developers to use [Twig Templates](http://twig.sensiolabs.org/) as an alternative to the standard opencart template method. Templates with the `.twig` extension are rendered using the Twig engine and are kept in the same `template` directory as standard templates. Backwards compatibility has been maintained as `tpl` and `twig` templates can be rendered together.

### Example - `common/home.tpl` as `common/home.twig`

```php
/* common/home.tpl */

```

```jinja
{# common/home.twig #}

{% if column_left and column_right %}
{% set class = 'col-sm-6' %}
{% elseif column_left or column_right %}
{% set class = 'col-sm-9' %}
{% else %}
{% set class = 'col-sm-12' %}
{% endif %}

{{ header }}

{{ column_left }}

{{ content_top }}{{ content_bottom }}

{{ column_right }}

{{ footer }}
```

### Notes

**auto escaping** - Since rendered child templates are stored in variables and included in the main controller template, auto escaping had to be turned off. Otherwise `` in the `$header` variable would be rendered as `<html>`

**`auto_reload`** - during development developers will want to enable `auto_reload` in the `Twig_Environment` object created in `system/library/twig.php`

```php
$this->environment = new Twig_Environment($loader, array(
'autoescape' => false,
'cache' => DIR_CACHE . 'templates_c',
'strict_variables' => false,
'auto_reload' => true
));
```

**composer** - I'd much rather have included Twig as a dependency using composer but I feel doing something like that would have a much larger impact on packaging opencart for both development and distribution. That's really not something I want to take on.