Documentation

Environment
in package

Stores the Twig configuration and renders templates.

Tags
author

Fabien Potencier fabien@symfony.com

Table of Contents

EXTRA_VERSION  = ''
MAJOR_VERSION  = 2
MINOR_VERSION  = 16
RELEASE_VERSION  = 0
VERSION  = '2.16.0'
VERSION_ID  = 21600
$autoReload  : mixed
$baseTemplateClass  : mixed
$cache  : mixed
$charset  : mixed
$compiler  : mixed
$debug  : mixed
$extensionSet  : mixed
$globals  : mixed
$lexer  : mixed
$loadedTemplates  : mixed
$loader  : mixed
$optionsHash  : mixed
$originalCache  : mixed
$parser  : mixed
$resolvedGlobals  : mixed
$runtimeLoaders  : mixed
$runtimes  : mixed
$strictVariables  : mixed
$templateClassPrefix  : mixed
__construct()  : mixed
Constructor.
addExtension()  : mixed
addFilter()  : mixed
addFunction()  : mixed
addGlobal()  : mixed
Registers a Global.
addNodeVisitor()  : mixed
addRuntimeLoader()  : mixed
Adds a runtime loader.
addTest()  : mixed
addTokenParser()  : mixed
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()  : mixed
Disables the auto_reload option.
disableDebug()  : mixed
Disables debugging mode.
disableStrictVariables()  : mixed
Disables the strict_variables option.
display()  : mixed
Displays a template.
enableAutoReload()  : mixed
Enables the auto_reload option.
enableDebug()  : mixed
Enables debugging mode.
enableStrictVariables()  : mixed
Enables the strict_variables option.
getBaseTemplateClass()  : string
Gets the base template class for compiled templates.
getCache()  : CacheInterface|string|false
Gets the current cache implementation.
getCharset()  : string
Gets the default template charset.
getExtension()  : ExtensionInterface
Gets an extension by class name.
getExtensions()  : array<string|int, ExtensionInterface>
Returns all registered extensions.
getLoader()  : LoaderInterface
Gets the Loader instance.
getRuntime()  : object
Returns the runtime implementation of a Twig element (filter/function/test).
hasExtension()  : bool
Returns true if the given extension is registered.
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>
Merges a context with the defined globals.
parse()  : ModuleNode
Converts a token stream to a node tree.
registerUndefinedFilterCallback()  : mixed
registerUndefinedFunctionCallback()  : mixed
render()  : string
Renders a template.
resolveTemplate()  : TemplateWrapper|Template
Tries to load a template consecutively from an array.
setBaseTemplateClass()  : mixed
Sets the base template class for compiled templates.
setCache()  : mixed
Sets the current cache implementation.
setCharset()  : mixed
Sets the default template charset.
setCompiler()  : mixed
setExtensions()  : mixed
Registers an array of extensions.
setLexer()  : mixed
setLoader()  : mixed
setParser()  : mixed
tokenize()  : TokenStream
Tokenizes a source code.
updateOptionsHash()  : mixed

Constants

Properties

$templateClassPrefix

private mixed $templateClassPrefix = '__TwigTemplate_'

Methods

__construct()

Constructor.

public __construct(LoaderInterface $loader[, 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).

  • base_template_class: The base template class to use for generated templates (default to \Twig\Template).

  • 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).

Parameters
$loader : LoaderInterface
$options : mixed = []
Return values
mixed

addGlobal()

Registers a Global.

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

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

Parameters
$name : string

The global name

$value : mixed

The global value

Return values
mixed

compile()

Compiles a node and returns the PHP code.

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

The compiled PHP source code

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

The compiled PHP source code

createTemplate()

Creates a template from source.

public createTemplate(string $template[, string $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

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

A template instance representing the given template name

disableAutoReload()

Disables the auto_reload option.

public disableAutoReload() : mixed
Return values
mixed

disableDebug()

Disables debugging mode.

public disableDebug() : mixed
Return values
mixed

disableStrictVariables()

Disables the strict_variables option.

public disableStrictVariables() : mixed
Return values
mixed

display()

Displays a template.

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

The template name

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

An array of parameters to pass to the template

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
mixed

enableAutoReload()

Enables the auto_reload option.

public enableAutoReload() : mixed
Return values
mixed

enableDebug()

Enables debugging mode.

public enableDebug() : mixed
Return values
mixed

enableStrictVariables()

Enables the strict_variables option.

public enableStrictVariables() : mixed
Return values
mixed

getBaseTemplateClass()

Gets the base template class for compiled templates.

public getBaseTemplateClass() : string
Return values
string

The base template class name

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()

Gets the default template charset.

public getCharset() : string
Return values
string

The default charset

getExtensions()

Returns all registered extensions.

public getExtensions() : array<string|int, ExtensionInterface>
Return values
array<string|int, ExtensionInterface>

An array of extensions (keys are for internal usage only and should not be relied on)

getRuntime()

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

public getRuntime(string $class) : object
Parameters
$class : string

A runtime class name

Tags
throws
RuntimeError

When the template cannot be found

Return values
object

The runtime implementation

hasExtension()

Returns true if the given extension is registered.

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

The extension class name

Return values
bool

Whether the extension is registered or not

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

The template name

$time : int

The last modification time of the cached template

Return values
bool

true if the template is fresh, false otherwise

mergeGlobals()

Merges a context with the defined globals.

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

An array representing the context

Return values
array<string|int, mixed>

The context merged with the globals

registerUndefinedFilterCallback()

public registerUndefinedFilterCallback(callable $callable) : mixed
Parameters
$callable : callable
Return values
mixed

registerUndefinedFunctionCallback()

public registerUndefinedFunctionCallback(callable $callable) : mixed
Parameters
$callable : callable
Return values
mixed

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> = []

An array of parameters to pass to the template

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

The rendered template

resolveTemplate()

Tries to load a template consecutively from an array.

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

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

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

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|Template

setBaseTemplateClass()

Sets the base template class for compiled templates.

public setBaseTemplateClass(string $class) : mixed
Parameters
$class : string

The base template class name

Return values
mixed

setCache()

Sets the current cache implementation.

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

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

Return values
mixed

setCharset()

Sets the default template charset.

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

The default charset

Return values
mixed

setExtensions()

Registers an array of extensions.

public setExtensions(array<string|int, mixed> $extensions) : mixed
Parameters
$extensions : array<string|int, mixed>

An array of extensions

Return values
mixed

updateOptionsHash()

private updateOptionsHash() : mixed
Return values
mixed

Search results