You are reading the documentation for the 1.x version. Switch to the current version 2.x.

Last updated: Fri, 29 May 2020 14:19

We provide a Twig package. Twig is a PHP template engine with is own syntax.

Integration in Berlioz

If you use the main repository of Berlioz : berlioz/berlioz, the package it's already available.

If you use customizable repositories, you need to install the package by yourself:

composer require berlioz/twig-package

Usage

In controllers, the method render(string $name, array $variables = []): string is available to process of the rendering of a template file.

Example inside controllers methods:

// In controller method
$rendering = $this->render(
    'my-template/path/file.html.twig',
    ['myVar' => 'value']
);

Example outside controllers:

/** @var \Berlioz\Package\Twig\Twig $twig */
$twig = $this->getCore()->getServiceContainer()->get(\Berlioz\Package\Twig\Twig::class);
$rendering = $twig->render(
    'my-template/path/file.html.twig',
    ['myVar' => 'value']
);

Methods

Twig package have some methods:

  • Twig::render(string $name, array $variables = []): string

    Add global variable.

  • Twig::hasBlock(string $name, string $blockName): bool

    Has block in template?

  • Twig::renderBlock(string $name, string $blockName, array $variables = []): string

    Render a block in template.

Configuration

Default configuration

{
    "berlioz": {
        "directories": {
            "templates": "%berlioz.directories.app%/templates"
        }
    },
    "twig": {
        "paths": {
            "__main__": "%berlioz.directories.templates%"
        },
        "options": {
            "cache": "%berlioz.directories.cache%/twig",
            "debug": "%berlioz.debug.enable%"
        },
        "extensions": [
            "Berlioz\\Package\\Twig\\TwigExtension"
        ],
        "globals": {}
    }
}

Cache

The internal cache of Twig is use, and can be disabled:

{
    "twig": {
        "options": {
            "cache": false
        }
    }
}