Classes, interfaces and traits

Compiler

Compiles a node to PHP code.

« More »

FileExtensionEscapingStrategy

Default autoescaping strategy based on file names.

This strategy sets the HTML as the default autoescaping strategy, but changes it based on the template name.

Note that there is no runtime performance impact as the default autoescaping strategy is set at compilation time.

« More »

Markup

Marks a content as safe.

« More »

NodeTraverser

A node traverser.

It visits all nodes and their children and calls the given visitor for each.

« More »

Parser

Default parser implementation.

« More »

Source

Holds information about a non-compiled Twig template.

« More »

Template

Default base class for compiled templates.

This class is an implementation detail of how template compilation currently works, which might change. It should never be used directly. Use $twig->load() instead, which returns an instance of \Twig\TemplateWrapper.

« More »

TemplateWrapper

Exposes a template to userland.

« More »

TokenStream

Represents a token stream.

« More »

TwigFilter

Represents a template filter.

« More »

TwigFunction

Represents a template function.

« More »

TwigTest

Represents a template test.

« More »

Classes, interfaces and traits

DeprecationCollector

« More »

TemplateDirIterator

« More »

Classes, interfaces and traits

BaseDumper

« More »

BlackfireDumper

« More »

HtmlDumper

« More »

TextDumper

« More »

Classes, interfaces and traits

EnterProfileNode

Represents a profile enter node.

« More »

LeaveProfileNode

Represents a profile leave node.

« More »

Classes, interfaces and traits

ProfilerNodeVisitor

Used to make node visitors compatible with Twig 1.x and 2.x.

To be removed in Twig 3.1.

« More »

Classes, interfaces and traits

ExtensionInterface

Interface implemented by extension classes.

« More »

GlobalsInterface

Enables usage of the deprecated Twig\Extension\AbstractExtension::getGlobals() method.

Explicitly implement this interface if you really need to implement the deprecated getGlobals() method in your extensions.

« More »

InitRuntimeInterface

Enables usage of the deprecated Twig\Extension\AbstractExtension::initRuntime() method.

Explicitly implement this interface if you really need to implement the deprecated initRuntime() method in your extensions.

« More »

RuntimeExtensionInterface

« More »

AbstractExtension

« More »

CoreExtension

« More »

DebugExtension

« More »

EscaperExtension

« More »

OptimizerExtension

« More »

ProfilerExtension

« More »

SandboxExtension

« More »

StagingExtension

Internal class.

This class is used by \Twig\Environment as a staging area and must not be used directly.

« More »

StringLoaderExtension

« More »

Classes, interfaces and traits

Error

Twig base exception.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

LoaderError

Exception thrown when an error occurs during template loading.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

RuntimeError

Exception thrown when an error occurs at runtime.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SyntaxError

\Exception thrown when a syntax error occurs during lexing or parsing of a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

Classes, interfaces and traits

NodeCaptureInterface

Represents a node that captures any nested displayable nodes.

« More »

NodeOutputInterface

Represents a displayable node in the AST.

« More »

AutoEscapeNode

Represents an autoescape node.

The value is the escaping strategy (can be html, js, ...)

The true value is equivalent to html.

If autoescaping is disabled, then the value is false.

« More »

BlockNode

Represents a block node.

« More »

BlockReferenceNode

Represents a block call node.

« More »

BodyNode

Represents a body node.

« More »

CheckSecurityCallNode

Represents a node in the AST.

« More »

CheckSecurityNode

Represents a node in the AST.

« More »

CheckToStringNode

Checks if casting an expression to __toString() is allowed by the sandbox.

For instance, when there is a simple Print statement, like {{ article }}, and if the sandbox is enabled, we need to check that the __toString() method is allowed if 'article' is an object. The same goes for {{ article|upper }} or {{ random(article) }}

« More »

DeprecatedNode

Represents a deprecated node.

« More »

DoNode

Represents a do node.

« More »

FlushNode

Represents a flush node.

« More »

ForLoopNode

Internal node used by the for node.

« More »

ImportNode

Represents an import node.

« More »

Node

Represents a node in the AST.

« More »

PrintNode

Represents a node that outputs an expression.

« More »

SandboxedPrintNode

Adds a check for the __toString() method when the variable is an object and the sandbox is activated.

When there is a simple Print statement, like {{ article }}, and if the sandbox is enabled, we need to check that the __toString() method is allowed if 'article' is an object.

Not used anymore, to be deprecated in 2.x and removed in 3.0

« More »

SandboxNode

Represents a sandbox node.

« More »

SetNode

Represents a set node.

« More »

SetTempNode

Represents a node in the AST.

« More »

SpacelessNode

Represents a spaceless node.

It removes spaces between HTML tags.

« More »

TextNode

Represents a text node.

« More »

Classes, interfaces and traits

AbstractExpression

Abstract class for all nodes that represents an expression.

« More »

ArrayExpression

Abstract class for all nodes that represents an expression.

« More »

ArrowFunctionExpression

Represents an arrow function.

« More »

AssignNameExpression

Abstract class for all nodes that represents an expression.

« More »

BlockReferenceExpression

Represents a block call node.

« More »

CallExpression

Abstract class for all nodes that represents an expression.

« More »

ConditionalExpression

Abstract class for all nodes that represents an expression.

« More »

ConstantExpression

Abstract class for all nodes that represents an expression.

« More »

FilterExpression

Abstract class for all nodes that represents an expression.

« More »

FunctionExpression

Abstract class for all nodes that represents an expression.

« More »

InlinePrint

Abstract class for all nodes that represents an expression.

« More »

MethodCallExpression

Abstract class for all nodes that represents an expression.

« More »

NameExpression

Abstract class for all nodes that represents an expression.

« More »

NullCoalesceExpression

Abstract class for all nodes that represents an expression.

« More »

ParentExpression

Represents a parent node.

« More »

TempNameExpression

Abstract class for all nodes that represents an expression.

« More »

Classes, interfaces and traits

ConstantTest

Checks if a variable is the exact same value as a constant.

{% if post.status is constant('Post::PUBLISHED') %} the status attribute is exactly the same as Post::PUBLISHED {% endif %}

« More »

DivisiblebyTest

Checks if a variable is divisible by a number.

{% if loop.index is divisible by(3) %}

« More »

EvenTest

Checks if a number is even.

{{ var is even }}

« More »

NullTest

Checks that a variable is null.

{{ var is none }}

« More »

OddTest

Checks if a number is odd.

{{ var is odd }}

« More »

SameasTest

Checks if a variable is the same as another one (=== in PHP).

« More »

Classes, interfaces and traits

AbstractUnary

Abstract class for all nodes that represents an expression.

« More »

NegUnary

Abstract class for all nodes that represents an expression.

« More »

NotUnary

Abstract class for all nodes that represents an expression.

« More »

PosUnary

Abstract class for all nodes that represents an expression.

« More »

Classes, interfaces and traits

AbstractBinary

Abstract class for all nodes that represents an expression.

« More »

AddBinary

Abstract class for all nodes that represents an expression.

« More »

AndBinary

Abstract class for all nodes that represents an expression.

« More »

BitwiseAndBinary

Abstract class for all nodes that represents an expression.

« More »

BitwiseOrBinary

Abstract class for all nodes that represents an expression.

« More »

BitwiseXorBinary

Abstract class for all nodes that represents an expression.

« More »

ConcatBinary

Abstract class for all nodes that represents an expression.

« More »

DivBinary

Abstract class for all nodes that represents an expression.

« More »

EndsWithBinary

Abstract class for all nodes that represents an expression.

« More »

EqualBinary

Abstract class for all nodes that represents an expression.

« More »

FloorDivBinary

Abstract class for all nodes that represents an expression.

« More »

GreaterBinary

Abstract class for all nodes that represents an expression.

« More »

GreaterEqualBinary

Abstract class for all nodes that represents an expression.

« More »

InBinary

Abstract class for all nodes that represents an expression.

« More »

LessBinary

Abstract class for all nodes that represents an expression.

« More »

LessEqualBinary

Abstract class for all nodes that represents an expression.

« More »

MatchesBinary

Abstract class for all nodes that represents an expression.

« More »

ModBinary

Abstract class for all nodes that represents an expression.

« More »

MulBinary

Abstract class for all nodes that represents an expression.

« More »

NotEqualBinary

Abstract class for all nodes that represents an expression.

« More »

NotInBinary

Abstract class for all nodes that represents an expression.

« More »

OrBinary

Abstract class for all nodes that represents an expression.

« More »

PowerBinary

Abstract class for all nodes that represents an expression.

« More »

RangeBinary

Abstract class for all nodes that represents an expression.

« More »

StartsWithBinary

Abstract class for all nodes that represents an expression.

« More »

SubBinary

Abstract class for all nodes that represents an expression.

« More »

Classes, interfaces and traits

DefaultFilter

Returns the value or the default value when it is undefined or empty.

{{ var.foo|default('foo item on var is not defined') }}

« More »

Classes, interfaces and traits

CacheInterface

Interface implemented by cache classes.

It is highly recommended to always store templates on the filesystem to benefit from the PHP opcode cache. This interface is mostly useful if you need to implement a custom strategy for storing templates on the filesystem.

« More »

NullCache

Implements a no-cache strategy.

« More »

Classes, interfaces and traits

SecurityPolicyInterface

Interface that all security policy classes must implements.

« More »

SecurityError

Exception thrown when a security error occurs at runtime.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityNotAllowedFilterError

Exception thrown when a not allowed filter is used in a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityNotAllowedFunctionError

Exception thrown when a not allowed function is used in a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityNotAllowedMethodError

Exception thrown when a not allowed class method is used in a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityNotAllowedPropertyError

Exception thrown when a not allowed class property is used in a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityNotAllowedTagError

Exception thrown when a not allowed tag is used in a template.

This exception class and its children must only be used when an error occurs during the loading of a template, when a syntax error is detected in a template, or when rendering a template. Other errors must use regular PHP exception classes (like when the template cache directory is not writable for instance).

To help debugging template issues, this class tracks the original template name and line where the error occurred.

Whenever possible, you must set these information (original template name and line number) yourself by passing them to the constructor. If some or all these information are not available from where you throw the exception, then this class will guess them automatically (when the line number is set to -1 and/or the name is set to null). As this is a costly operation, this can be disabled by passing false for both the name and the line number when creating a new instance of this class.

« More »

SecurityPolicy

Represents a security policy which need to be enforced when sandbox mode is enabled.

« More »

Classes, interfaces and traits

ExistsLoaderInterface

Adds an exists() method for loaders.

« More »

LoaderInterface

Interface all loaders must implement.

« More »

SourceContextLoaderInterface

Adds a getSourceContext() method for loaders.

« More »

ArrayLoader

Loads a template from an array.

When using this loader with a cache mechanism, you should know that a new cache key is generated each time a template content "changes" (the cache key being the source code of the template). If you don't want to see your cache grows out of control, you need to take care of clearing the old cache file by yourself.

This loader should only be used for unit testing.

« More »

ChainLoader

Loads templates from other loaders.

« More »

Classes, interfaces and traits

RuntimeLoaderInterface

Creates runtime implementations for Twig elements (filters/functions/tests).

« More »

ContainerRuntimeLoader

Lazily loads Twig runtime implementations from a PSR-11 container.

Note that the runtime services MUST use their class names as identifiers.

« More »

FactoryRuntimeLoader

Lazy loads the runtime implementations for a Twig element.

« More »

Classes, interfaces and traits

IntegrationTestCase

Integration test helper.

« More »

NodeTestCase

« More »

Classes, interfaces and traits

NodeVisitorInterface

Interface for node visitor classes.

« More »

AbstractNodeVisitor

Used to make node visitors compatible with Twig 1.x and 2.x.

To be removed in Twig 3.1.

« More »

EscaperNodeVisitor

Used to make node visitors compatible with Twig 1.x and 2.x.

To be removed in Twig 3.1.

« More »

SafeAnalysisNodeVisitor

Used to make node visitors compatible with Twig 1.x and 2.x.

To be removed in Twig 3.1.

« More »

SandboxNodeVisitor

Used to make node visitors compatible with Twig 1.x and 2.x.

To be removed in Twig 3.1.

« More »

Classes, interfaces and traits

TokenParserInterface

Interface implemented by token parsers.

« More »

AbstractTokenParser

Base class for all token parsers.

« More »

ApplyTokenParser

Applies filters on a section of a template.

{% apply upper %} This text becomes uppercase {% endapply %}

« More »

AutoEscapeTokenParser

Marks a section of a template to be escaped or not.

{% autoescape true %} Everything will be automatically escaped in this block {% endautoescape %}

{% autoescape false %} Everything will be outputed as is in this block {% endautoescape %}

{% autoescape true js %} Everything will be automatically escaped in this block using the js escaping strategy {% endautoescape %}

« More »

BlockTokenParser

Marks a section of a template as being reusable.

{% block head %}

{% block title %}{% endblock %} - My Webpage

{% endblock %}

« More »

DeprecatedTokenParser

Deprecates a section of a template.

{% deprecated 'The "base.twig" template is deprecated, use "layout.twig" instead.' %} {% extends 'layout.html.twig' %}

« More »

DoTokenParser

Evaluates an expression, discarding the returned value.

« More »

EmbedTokenParser

Embeds a template.

{% include 'header.html' %} Body {% include 'footer.html' %}

« More »

ExtendsTokenParser

Extends a template by another one.

{% extends "base.html" %}

« More »

FilterTokenParser

Filters a section of a template by applying filters.

{% filter upper %} This text becomes uppercase {% endfilter %}

« More »

FlushTokenParser

Flushes the output to the client.

« More »

ForTokenParser

Loops over each item of a sequence.

    {% for user in users %}
  • {{ user.username|e }}
  • {% endfor %}
« More »

FromTokenParser

Imports macros.

{% from 'forms.html' import forms %}

« More »

IfTokenParser

Tests a condition.

{% if users %}

    {% for user in users %}
  • {{ user.username|e }}
  • {% endfor %}

{% endif %}

« More »

ImportTokenParser

Imports macros.

{% import 'forms.html' as forms %}

« More »

IncludeTokenParser

Includes a template.

{% include 'header.html' %} Body {% include 'footer.html' %}

« More »

MacroTokenParser

Defines a macro.

{% macro input(name, value, type, size) %}

{% endmacro %}

« More »

SandboxTokenParser

Marks a section of a template as untrusted code that must be evaluated in the sandbox mode.

{% sandbox %} {% include 'user.html' %} {% endsandbox %}

« More »

SetTokenParser

Defines a variable.

{% set foo = 'foo' %} {% set foo = [1, 2] %} {% set foo = {'foo': 'bar'} %} {% set foo = 'foo' ~ 'bar' %} {% set foo, bar = 'foo', 'bar' %} {% set foo %}Some content{% endset %}

« More »

SpacelessTokenParser

Remove whitespaces between HTML tags.

{% spaceless %}

foo

{% endspaceless %} {# output will be

foo
#}

« More »

UseTokenParser

Imports blocks defined in another template into the current template.

{% extends "base.html" %}

{% use "blocks.html" %}

{% block title %}{% endblock %} {% block content %}{% endblock %}

« More »

WithTokenParser

Creates a nested scope.

« More »