Templates

Last updated: Wed, 08 Sep 2021 23:18

We provide a Twig package. Twig is a PHP template engine with its 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:

use Berlioz\Package\Twig\Twig;

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

Service inflector

An inflector is provided with interface: \Berlioz\Package\Twig\TwigAwareInterface.

For more information on inflectors, refers you to container inflectors.

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": "{config: berlioz.directories.app}/templates"
    }
  },
  "twig": {
    "paths": {
      "__main__": "{config: berlioz.directories.templates}",
      "Berlioz-TwigPackage": "{config: berlioz.directories.vendor}/berlioz/twig-package/resources"
    },
    "options": {
      "cache": "{config: berlioz.directories.cache}/twig",
      "optimizers": null
    },
    "extensions": [
      "Berlioz\\Package\\Twig\\Extension\\AssetExtension",
      "Berlioz\\Package\\Twig\\Extension\\DefaultExtension",
      "Berlioz\\Package\\Twig\\Extension\\RouterExtension"
    ],
    "globals": {}
  }
}

Cache

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

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