Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Tags types
You are reading the documentation for Twig 3.x. Switch to the documentation for Twig 1.x. 2.x.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

types

3.13

The types tag was added in Twig 3.13. This tag is experimental and can change based on usage and feedback.

The types tag declares the types of template variables.

To do this, specify a mapping of names to their types as strings.

Here is how to declare that is_correct is a boolean, while score is an integer (see note below):

1
2
3
4
{% types {
    is_correct: 'bool',
    score: 'int',
} %}

You can declare variables as optional by adding the ? suffix:

1
2
3
4
{% types {
    is_correct: 'bool',
    score?: 'int',
} %}

By default, this tag does not affect the template compilation or runtime behavior.

Its purpose is to enable designers and developers to document and specify the context's available and/or required variables. While Twig itself does not validate variables or their types, this tag enables extensions to do this.

Additionally, Twig extensions can analyze these tags to perform compile-time and runtime analysis of templates.

Note

The syntax for and contents of type strings are intentionally left out of scope.