Templates

Tomahawk uses Symfony's Php Engine and Twig for templates. A Delegating engine is used so you can use either or both the template engines mentioned.

The easiest way to use the Template component is to access to it through the container $container->get('templating').

Otherwise just add the following parameter to the construct method of your Controller Symfony\Component\Templating\EngineInterface and it will get injected in through the Service Container.

Template files must be in the Resources/views folder with you Bundle.

For more information on the PhpEngine please refer to the docs here. For more information on Twig please refer to the docs here.

Rendering A Template


A template name has the format 'Bundle:Section:view.engine' The engine can be omitted and defaults to .php.

Rendering Using Full Template Name

Below is an example of rendering a template using the full name:

Rendering Using Partial Template Name

You can omit the Section if the template is in the view root page:

Passing Parameters To The Template

You can pass parameters to the template file by passing an array as the second parameter to render.

Global Variables

Tomahawk also allows you to pass global variables to your templates. There are 2 ways to do it.

One way is to to add them to the app/templating.php config. e.g:

Another way is to tag a service in your container with php.global when using Php Engine or twig.global when using twig

Reserved Global Variable

There is a reserved global variable called app. This is an instance of \Tomahawk\Templating\GlobalVariables and gives you access to the request, current logged in user (if any), session, environment and if debug mode is enabled.

The following methods are available:

  • $app->getRequest()
  • $app->getUser()
  • $app->getSession()
  • $app->getEnvironment()
  • $app->isDebug()

Php Engine


Php Engine is the easiest templating engine to use. It uses a global $view variable which is an instance of the Php Engine.

You can read more about Symfony templating here here.

Helpers


Php Engine has a few helpers you can use when creating templates:

Slots Helper


Tomahawk uses the Slot helper from Symfony. The slot helper is used to define and output different parts of a template. You can read more about the Slot helper here.

You can use the Slots helper by accessing $view['slots']. There are 2 sets of syntax for defining slots.

The first is for short bits of text, such as a page title

You can also use the extended syntax.

Block Helper


The block helper is an more advanced slot helper that allows you to set default content. You can use the Block helper by accessing $view['blocks'].

To create a default content block (for example in a layout template) you would do the following:

Then to override in another template you would do the following:

Translator Helper


You can use the Translator helper by accessing $view['translator'] which will be an instance of \Symfony\Component\Translation\TranslatorInterface.

Url Generator Helper


You can use the Url helper by accessing $view['url'] which will be an instance of \Tomahawk\Url\UrlGeneratorInterface.

Example

Below is a simple example of creating a template:

And then using that layout in another template

Twig


Twig is an amazing templating engine from SensioLabs, the people that bought us the Symfony framework.

You can find documentation on Twig here.

Below is a simple example of creating a layout template:

And then using that layout in another template

Extensions

Tomahawk provides 2 extensions for Twig, Translator and Url Generator.

Translator Extension

The Translator extension internally uses the Symfony Translator which is an instance of \Symfony\Component\Translation\TranslatorInterface.

You can use them as follows:

Url Generator Extension

The Url Generator extension internally uses the Tomahawk Url Generator which is an instance of \Tomahawk\Url\UrlGeneratorInterface.

You can use them as follows: