Skip to content

twig rendering

Harmen Janssen edited this page Jun 28, 2016 · 2 revisions

Twig rendering

Garp supports Twig rendering, next to Zend Framework's own phtml templates.

Setting up

Currently the Garp Scaffold contains all you need for Twig rendering, so for new projects you don't really have to do anything.

Link up Composer

The Twig-to-Zend glue code comes from a fork of the Ano_ZFTwig project.
Configure composer.json to use the fork:

  "repositories": [{
    "type": "vcs",
    "url": "https://github.com/harmenjanssen/Ano_ZFTwig"
  }],
  "require": {
    "twig/twig": "^1.24",
    "ano/zf-twig": "master"
  }

Running composer update will install the fork.

Configure your project

Configure the following, for instance inside core.ini:

pluginPaths.Ano_Application_Resource = APPLICATION_PATH "/../vendor/ano/zf-twig/library/Ano/Application/Resource"

resources.view.engines.php.class = "Ano_View_Engine_PhpEngine"
resources.view.engines.php.viewSuffix = "phtml"

resources.view.engines.twig.class = "Ano_ZFTwig_View_Engine_TwigEngine"
resources.view.engines.twig.isDefault = 1
resources.view.engines.twig.viewSuffix = "twig"
resources.view.engines.twig.options.charset = "utf-8"
resources.view.engines.twig.options.strict_variables = 0
;resources.view.engines.twig.options.cache = APPLICATION_PATH "/../var/cache/twig"
resources.view.engines.twig.options.auto_escape = 1
resources.view.engines.twig.options.auto_reload = 1
resources.view.engines.twig.options.debug = 0
resources.view.engines.twig.options.trim_blocks = 1

resources.view.engines.twig.extensions.debug.class = "Twig_Extension_Debug"
resources.view.engines.twig.extensions.helper.class = "Ano_ZFTwig_Extension_HelperExtension"
resources.view.engines.twig.extensions.trans.class = "Ano_ZFTwig_Extension_TransExtension"

resources.view.engines.twig.globals.class = "Garp_ZFTwig_GlobalVariables"
resources.view.engines.twig.globals.name = "app"


; You probably want to enable debug for development:

[development : staging]

resources.view.engines.twig.options.debug = 1

Also, to instruct the Spawner to create a Twig file you have to configure the following:

spawn.js.modelLoaderFile = "/modules/default/views/scripts/partials/models.twig"

Create Twig templates

That's it! You can now write Twig templates for your project.
See the original documentation on the Ano_ZFTwig project for more specifics.

Globals, and accessing configuration

In any Twig template, there's a global app variable that provides often-used information:

{# Read current environment #}
{% if app.environment == 'production' %}
  ...
{% endif %}

{# Get any config value #}
{{ app.config.cdn.region }}

{# Get current project version (same as Garp_Semver) #}
{{ app.version }}

{# Get application path #}
{{ app.applicationPath }}

Another global variable that's available everywhere is zf. You can use this to access any view helpers you would normally find in $this in a regular template.

{{ zf.assetUrl('main.js') }}
{{ zf.snippet('my_snippet') }}

Rendering partials

Zend Framework's module system has been translated into Twig namespaces. The default module becomes the __main__ namespace, meaning you don't have to specify it in your include() calls.
Other modules can be specified using Twig's namespace syntax:

{# include partial from the default module #}
{{ include('partials/models.twig') }}

{# include partial from Garp's 'g' module #}
{{ include('@g/partials/snippet.twig') }}

Todo's

  • The zf.helper syntax works fine but it would be a little more elegant perhaps to move some helpers to a more native Twig-like construct
  • Figure out a good caching setup
Clone this wiki locally