Documentation

Environment
in package

Stores the Twig configuration and renders templates.

Tags
author

Fabien Potencier fabien@symfony.com

Table of Contents

Constants

EXTRA_VERSION  = ''
MAJOR_VERSION  = 3
MINOR_VERSION  = 22
RELEASE_VERSION  = 2
VERSION  = '3.22.2'
VERSION_ID  = 32202

Properties

$autoReload  : mixed
$cache  : mixed
$charset  : mixed
$compiler  : mixed
$debug  : mixed
$defaultRuntimeLoader  : mixed
$extensionSet  : mixed
$globals  : array<string, mixed>
$hotCache  : array<string|int, mixed>
$lexer  : mixed
$loadedTemplates  : mixed
$loader  : mixed
$optionsHash  : mixed
$originalCache  : mixed
$parser  : mixed
$resolvedGlobals  : mixed
$runtimeLoaders  : mixed
$runtimes  : mixed
$strictVariables  : mixed
$useYield  : bool

Methods

__construct()  : mixed
Constructor.
addExtension()  : void
addFilter()  : void
addFunction()  : void
addGlobal()  : void
Registers a Global.
addNodeVisitor()  : void
addRuntimeLoader()  : void
addTest()  : void
addTokenParser()  : void
compile()  : string
Compiles a node and returns the PHP code.
compileSource()  : string
Compiles a template source code.
createTemplate()  : TemplateWrapper
Creates a template from source.
disableAutoReload()  : void
Disables the auto_reload option.
disableDebug()  : void
Disables debugging mode.
disableStrictVariables()  : void
Disables the strict_variables option.
display()  : void
Displays a template.
enableAutoReload()  : void
Enables the auto_reload option.
enableDebug()  : void
Enables debugging mode.
enableStrictVariables()  : void
Enables the strict_variables option.
getCache()  : CacheInterface|string|false
Gets the current cache implementation.
getCharset()  : string
getExtension()  : TExtension
getExtensions()  : array<string|int, ExtensionInterface>
getGlobals()  : array<string, mixed>
getLoader()  : LoaderInterface
getRuntime()  : TRuntime
Returns the runtime implementation of a Twig element (filter/function/tag/test).
hasExtension()  : bool
isAutoReload()  : bool
Checks if the auto_reload option is enabled.
isDebug()  : bool
Checks if debug mode is enabled.
isStrictVariables()  : bool
Checks if the strict_variables option is enabled.
isTemplateFresh()  : bool
Returns true if the template is still fresh.
load()  : TemplateWrapper
Loads a template.
mergeGlobals()  : array<string|int, mixed>
parse()  : ModuleNode
Converts a token stream to a node tree.
registerUndefinedFilterCallback()  : void
registerUndefinedFunctionCallback()  : void
registerUndefinedTestCallback()  : void
registerUndefinedTokenParserCallback()  : void
removeCache()  : void
render()  : string
Renders a template.
resetGlobals()  : void
resolveTemplate()  : TemplateWrapper
Tries to load a template consecutively from an array.
setCache()  : void
Sets the current cache implementation.
setCharset()  : void
setCompiler()  : void
setExtensions()  : void
setLexer()  : void
setLoader()  : void
setParser()  : void
tokenize()  : TokenStream
updateOptionsHash()  : void

Constants

Properties

$defaultRuntimeLoader

private mixed $defaultRuntimeLoader

$hotCache

private array<string|int, mixed> $hotCache = []

Methods

__construct()

Constructor.

public __construct(LoaderInterface $loader[, array<string|int, mixed> $options = [] ]) : mixed

Available options:

  • debug: When set to true, it automatically set "auto_reload" to true as well (default to false).

  • charset: The charset used by the templates (default to UTF-8).

  • cache: An absolute path where to store the compiled templates, a \Twig\Cache\CacheInterface implementation, or false to disable compilation cache (default).

  • auto_reload: Whether to reload the template if the original source changed. If you don't provide the auto_reload option, it will be determined automatically based on the debug value.

  • strict_variables: Whether to ignore invalid variables in templates (default to false).

  • autoescape: Whether to enable auto-escaping (default to html): * false: disable auto-escaping * html, js: set the autoescaping to one of the supported strategies * name: set the autoescaping strategy based on the template name extension * PHP callback: a PHP callback that returns an escaping strategy based on the template "name"

  • optimizations: A flag that indicates which optimizations to apply (default to -1 which means that all optimizations are enabled; set it to 0 to disable).

  • use_yield: true: forces templates to exclusively use "yield" instead of "echo" (all extensions must be yield ready) false (default): allows templates to use a mix of "yield" and "echo" calls to allow for a progressive migration Switch to "true" when possible as this will be the only supported mode in Twig 4.0

Parameters
$loader : LoaderInterface
$options : array<string|int, mixed> = []

addGlobal()

Registers a Global.

public addGlobal(string $name, mixed $value) : void

New globals can be added before compiling or rendering a template; but after, you can only update existing globals.

Parameters
$name : string
$value : mixed

The global value

compile()

Compiles a node and returns the PHP code.

public compile(Node $node) : string
Parameters
$node : Node
Return values
string

compileSource()

Compiles a template source code.

public compileSource(Source $source) : string
Parameters
$source : Source
Tags
throws
SyntaxError

When there was an error during tokenizing, parsing or compiling

Return values
string

createTemplate()

Creates a template from source.

public createTemplate(string $template[, string|null $name = null ]) : TemplateWrapper

This method should not be used as a generic way to load templates.

Parameters
$template : string

The template source

$name : string|null = null

An optional name of the template to be used in error messages

Tags
throws
LoaderError

When the template cannot be found

throws
SyntaxError

When an error occurred during compilation

Return values
TemplateWrapper

disableAutoReload()

Disables the auto_reload option.

public disableAutoReload() : void

disableDebug()

Disables debugging mode.

public disableDebug() : void

disableStrictVariables()

Disables the strict_variables option.

public disableStrictVariables() : void

display()

Displays a template.

public display(string|TemplateWrapper $name[, array<string|int, mixed> $context = [] ]) : void
Parameters
$name : string|TemplateWrapper

The template name

$context : array<string|int, mixed> = []
Tags
throws
LoaderError

When the template cannot be found

throws
SyntaxError

When an error occurred during compilation

throws
RuntimeError

When an error occurred during rendering

enableAutoReload()

Enables the auto_reload option.

public enableAutoReload() : void

enableDebug()

Enables debugging mode.

public enableDebug() : void

enableStrictVariables()

Enables the strict_variables option.

public enableStrictVariables() : void

getCache()

Gets the current cache implementation.

public getCache([bool $original = true ]) : CacheInterface|string|false
Parameters
$original : bool = true

Whether to return the original cache option or the real cache instance

Return values
CacheInterface|string|false

A Twig\Cache\CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache

getCharset()

public getCharset() : string
Return values
string

getExtension()

public getExtension(TExtension> $class) : TExtension
Parameters
$class : TExtension>
Tags
template
Return values
TExtension

getGlobals()

public getGlobals() : array<string, mixed>
Return values
array<string, mixed>

getRuntime()

Returns the runtime implementation of a Twig element (filter/function/tag/test).

public getRuntime(TRuntime> $class) : TRuntime
Parameters
$class : TRuntime>

A runtime class name

Tags
template
throws
RuntimeError

When the template cannot be found

Return values
TRuntime

The runtime implementation

hasExtension()

public hasExtension(string $class) : bool
Parameters
$class : string
Return values
bool

isAutoReload()

Checks if the auto_reload option is enabled.

public isAutoReload() : bool
Return values
bool

true if auto_reload is enabled, false otherwise

isDebug()

Checks if debug mode is enabled.

public isDebug() : bool
Return values
bool

true if debug mode is enabled, false otherwise

isStrictVariables()

Checks if the strict_variables option is enabled.

public isStrictVariables() : bool
Return values
bool

true if strict_variables is enabled, false otherwise

isTemplateFresh()

Returns true if the template is still fresh.

public isTemplateFresh(string $name, int $time) : bool

Besides checking the loader for freshness information, this method also checks if the enabled extensions have not changed.

Parameters
$name : string
$time : int

The last modification time of the cached template

Return values
bool

mergeGlobals()

public mergeGlobals(array<string|int, mixed> $context) : array<string|int, mixed>

since Twig 3.14

Parameters
$context : array<string|int, mixed>
Return values
array<string|int, mixed>

registerUndefinedFilterCallback()

public registerUndefinedFilterCallback( Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array $callable) : void
Parameters
$callable : Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array

registerUndefinedFunctionCallback()

public registerUndefinedFunctionCallback( Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array $callable) : void
Parameters
$callable : Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array

registerUndefinedTestCallback()

public registerUndefinedTestCallback( Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array $callable) : void
Parameters
$callable : Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array

registerUndefinedTokenParserCallback()

public registerUndefinedTokenParserCallback( Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array $callable) : void
Parameters
$callable : Warning: Array to string conversion in C:\xampp\htdocs\vendor\phpdocumentor\phpdocumentor\src\phpDocumentor\Transformer\Writer\Twig\LinkRenderer\CallableAdapter.php on line 78 callable(string): Array

removeCache()

public removeCache(string $name) : void
Parameters
$name : string

render()

Renders a template.

public render(string|TemplateWrapper $name[, array<string|int, mixed> $context = [] ]) : string
Parameters
$name : string|TemplateWrapper

The template name

$context : array<string|int, mixed> = []
Tags
throws
LoaderError

When the template cannot be found

throws
SyntaxError

When an error occurred during compilation

throws
RuntimeError

When an error occurred during rendering

Return values
string

resolveTemplate()

Tries to load a template consecutively from an array.

public resolveTemplate(string|TemplateWrapper|array<string|int, string|TemplateWrapper$names) : TemplateWrapper

Similar to load() but it also accepts instances of \Twig\TemplateWrapper and an array of templates where each is tried to be loaded.

Parameters
$names : string|TemplateWrapper|array<string|int, string|TemplateWrapper>

A template or an array of templates to try consecutively

Tags
throws
LoaderError

When none of the templates can be found

throws
SyntaxError

When an error occurred during compilation

Return values
TemplateWrapper

setCache()

Sets the current cache implementation.

public setCache(CacheInterface|string|false $cache) : void
Parameters
$cache : CacheInterface|string|false

A Twig\Cache\CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache

setCharset()

public setCharset(string $charset) : void
Parameters
$charset : string

updateOptionsHash()

private updateOptionsHash() : void

        
On this page

Search results