Twig API
Class

Twig_Environment

class Twig_Environment

Stores the Twig configuration.

Constants

VERSION

Methods

__construct(Twig_LoaderInterface $loader = null, array $options = array())

Constructor.

string getBaseTemplateClass()

Gets the base template class for compiled templates.

setBaseTemplateClass(string $class)

Sets the base template class for compiled templates.

enableDebug()

Enables debugging mode.

disableDebug()

Disables debugging mode.

bool isDebug()

Checks if debug mode is enabled.

enableAutoReload()

Enables the auto_reload option.

disableAutoReload()

Disables the auto_reload option.

bool isAutoReload()

Checks if the auto_reload option is enabled.

enableStrictVariables()

Enables the strict_variables option.

disableStrictVariables()

Disables the strict_variables option.

bool isStrictVariables()

Checks if the strict_variables option is enabled.

string|false getCache()

Gets the cache directory or false if cache is disabled.

setCache(string|false $cache)

Sets the cache directory or false if cache is disabled.

string|false getCacheFilename(string $name)

Gets the cache filename for a given template.

string getTemplateClass(string $name, int $index = null)

Gets the template class associated with the given string.

string getTemplateClassPrefix()

Gets the template class prefix.

string render(string $name, array $context = array())

Renders a template.

display(string $name, array $context = array())

Displays a template.

Twig_TemplateInterface loadTemplate(string $name, int $index = null)

Loads a template by name.

bool isTemplateFresh(string $name, timestamp $time)

Returns true if the template is still fresh.

Twig_Template resolveTemplate(string|Twig_Template|array $names)

Tries to load a template consecutively from an array.

clearTemplateCache()

Clears the internal template cache.

clearCacheFiles()

Clears the template cache files on the filesystem.

Twig_LexerInterface getLexer()

Gets the Lexer instance.

setLexer(Twig_LexerInterface $lexer)

Sets the Lexer instance.

Twig_TokenStream tokenize(string $source, string $name = null)

Tokenizes a source code.

Twig_ParserInterface getParser()

Gets the Parser instance.

setParser(Twig_ParserInterface $parser)

Sets the Parser instance.

Twig_Node_Module parse(Twig_TokenStream $stream)

Converts a token stream to a node tree.

Twig_CompilerInterface getCompiler()

Gets the Compiler instance.

setCompiler(Twig_CompilerInterface $compiler)

Sets the Compiler instance.

string compile(Twig_NodeInterface $node)

Compiles a node and returns the PHP code.

string compileSource(string $source, string $name = null)

Compiles a template source code.

setLoader(Twig_LoaderInterface $loader)

Sets the Loader instance.

Twig_LoaderInterface getLoader()

Gets the Loader instance.

setCharset(string $charset)

Sets the default template charset.

string getCharset()

Gets the default template charset.

initRuntime()

Initializes the runtime environment.

bool hasExtension(string $name)

Returns true if the given extension is registered.

Twig_ExtensionInterface getExtension(string $name)

Gets an extension by name.

addExtension(Twig_ExtensionInterface $extension)

Registers an extension.

removeExtension(string $name)

Removes an extension by name.

setExtensions(array $extensions)

Registers an array of extensions.

array getExtensions()

Returns all registered extensions.

addTokenParser(Twig_TokenParserInterface $parser)

Registers a Token Parser.

Twig_TokenParserBrokerInterface getTokenParsers()

Gets the registered Token Parsers.

Twig_TokenParserInterface[] getTags()

Gets registered tags.

addNodeVisitor(Twig_NodeVisitorInterface $visitor)

Registers a Node Visitor.

Twig_NodeVisitorInterface[] getNodeVisitors()

Gets the registered Node Visitors.

addFilter(string|Twig_SimpleFilter $name, Twig_FilterInterface|Twig_SimpleFilter $filter = null)

Registers a Filter.

Twig_Filter|false getFilter(string $name)

Get a filter by name.

registerUndefinedFilterCallback($callable)

Twig_FilterInterface[] getFilters()

Gets the registered Filters.

addTest(string|Twig_SimpleTest $name, Twig_TestInterface|Twig_SimpleTest $test = null)

Registers a Test.

Twig_TestInterface[] getTests()

Gets the registered Tests.

Twig_Test|false getTest(string $name)

Gets a test by name.

addFunction(string|Twig_SimpleFunction $name, Twig_FunctionInterface|Twig_SimpleFunction $function = null)

Registers a Function.

Twig_Function|false getFunction(string $name)

Get a function by name.

registerUndefinedFunctionCallback($callable)

Twig_FunctionInterface[] getFunctions()

Gets registered functions.

addGlobal(string $name, mixed $value)

Registers a Global.

array getGlobals()

Gets the registered Globals.

array mergeGlobals(array $context)

Merges a context with the defined globals.

array getUnaryOperators()

Gets the registered unary Operators.

array getBinaryOperators()

Gets the registered binary Operators.

computeAlternatives($name, $items)

Details

at line 84
public __construct(Twig_LoaderInterface $loader = null, array $options = array())

Constructor.

Available options:

Parameters

Twig_LoaderInterface $loader A Twig_LoaderInterface instance
array $options An array of options

at line 123
public string getBaseTemplateClass()

Gets the base template class for compiled templates.

Return Value

string The base template class name

at line 133
public setBaseTemplateClass(string $class)

Sets the base template class for compiled templates.

Parameters

string $class The base template class name

at line 141
public enableDebug()

Enables debugging mode.

at line 149
public disableDebug()

Disables debugging mode.

at line 159
public bool isDebug()

Checks if debug mode is enabled.

Return Value

bool true if debug mode is enabled, false otherwise

at line 167
public enableAutoReload()

Enables the auto_reload option.

at line 175
public disableAutoReload()

Disables the auto_reload option.

at line 185
public bool isAutoReload()

Checks if the auto_reload option is enabled.

Return Value

bool true if auto_reload is enabled, false otherwise

at line 193
public enableStrictVariables()

Enables the strict_variables option.

at line 201
public disableStrictVariables()

Disables the strict_variables option.

at line 211
public bool isStrictVariables()

Checks if the strict_variables option is enabled.

Return Value

bool true if strict_variables is enabled, false otherwise

at line 221
public string|false getCache()

Gets the cache directory or false if cache is disabled.

Return Value

string|false

at line 232
public setCache(string|false $cache)

Sets the cache directory or false if cache is disabled.

Parameters

string|false $cache The absolute path to the compiled templates, or false to disable cache

at line 244
public string|false getCacheFilename(string $name)

Gets the cache filename for a given template.

Parameters

string $name The template name

Return Value

string|false The cache file name or false when caching is disabled

at line 263
public string getTemplateClass(string $name, int $index = null)

Gets the template class associated with the given string.

Parameters

string $name The name for which to calculate the template class name
int $index The index if it is an embedded template

Return Value

string The template class name

at line 273
public string getTemplateClassPrefix()

Gets the template class prefix.

Return Value

string The template class prefix

at line 290
public string render(string $name, array $context = array())

Renders a template.

Parameters

string $name The template name
array $context An array of parameters to pass to the template

Return Value

string The rendered template

Exceptions

Twig_Error_Loader When the template cannot be found
Twig_Error_Syntax When an error occurred during compilation
Twig_Error_Runtime When an error occurred during rendering

at line 305
public display(string $name, array $context = array())

Displays a template.

Parameters

string $name The template name
array $context An array of parameters to pass to the template

Exceptions

Twig_Error_Loader When the template cannot be found
Twig_Error_Syntax When an error occurred during compilation
Twig_Error_Runtime When an error occurred during rendering

at line 321
public Twig_TemplateInterface loadTemplate(string $name, int $index = null)

Loads a template by name.

Parameters

string $name The template name
int $index The index if it is an embedded template

Return Value

Twig_TemplateInterface A template instance representing the given template name

Exceptions

Twig_Error_Loader When the template cannot be found
Twig_Error_Syntax When an error occurred during compilation

at line 360
public bool isTemplateFresh(string $name, timestamp $time)

Returns true if the template is still fresh.

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

Parameters

string $name The template name
timestamp $time The last modification time of the cached template

Return Value

bool true if the template is fresh, false otherwise

at line 385
public Twig_Template resolveTemplate(string|Twig_Template|array $names)

Tries to load a template consecutively from an array.

Similar to loadTemplate() but it also accepts Twig_TemplateInterface instances and an array of templates where each is tried to be loaded.

Parameters

string|Twig_Template|array $names A template or an array of templates to try consecutively

Return Value

Twig_Template

Exceptions

Twig_Error_Loader When none of the templates can be found
Twig_Error_Syntax When an error occurred during compilation

at line 412
public clearTemplateCache()

Clears the internal template cache.

at line 420
public clearCacheFiles()

Clears the template cache files on the filesystem.

at line 438
public Twig_LexerInterface getLexer()

Gets the Lexer instance.

Return Value

Twig_LexerInterface A Twig_LexerInterface instance

at line 452
public setLexer(Twig_LexerInterface $lexer)

Sets the Lexer instance.

Parameters

Twig_LexerInterface $lexer A Twig_LexerInterface instance

at line 467
public Twig_TokenStream tokenize(string $source, string $name = null)

Tokenizes a source code.

Parameters

string $source The template source code
string $name The template name

Return Value

Twig_TokenStream A Twig_TokenStream instance

Exceptions

Twig_Error_Syntax When the code is syntactically wrong

at line 477
public Twig_ParserInterface getParser()

Gets the Parser instance.

Return Value

Twig_ParserInterface A Twig_ParserInterface instance

at line 491
public setParser(Twig_ParserInterface $parser)

Sets the Parser instance.

Parameters

Twig_ParserInterface $parser A Twig_ParserInterface instance

at line 505
public Twig_Node_Module parse(Twig_TokenStream $stream)

Converts a token stream to a node tree.

Parameters

Twig_TokenStream $stream A token stream instance

Return Value

Twig_Node_Module A node tree

Exceptions

Twig_Error_Syntax When the token stream is syntactically or semantically wrong

at line 515
public Twig_CompilerInterface getCompiler()

Gets the Compiler instance.

Return Value

Twig_CompilerInterface A Twig_CompilerInterface instance

at line 529
public setCompiler(Twig_CompilerInterface $compiler)

Sets the Compiler instance.

Parameters

Twig_CompilerInterface $compiler A Twig_CompilerInterface instance

at line 541
public string compile(Twig_NodeInterface $node)

Compiles a node and returns the PHP code.

Parameters

Twig_NodeInterface $node A Twig_NodeInterface instance

Return Value

string The compiled PHP source code

at line 556
public string compileSource(string $source, string $name = null)

Compiles a template source code.

Parameters

string $source The template source code
string $name The template name

Return Value

string The compiled PHP source code

Exceptions

Twig_Error_Syntax When there was an error during tokenizing, parsing or compiling

at line 573
public setLoader(Twig_LoaderInterface $loader)

Sets the Loader instance.

Parameters

Twig_LoaderInterface $loader A Twig_LoaderInterface instance

at line 583
public Twig_LoaderInterface getLoader()

Gets the Loader instance.

Return Value

Twig_LoaderInterface A Twig_LoaderInterface instance

at line 597
public setCharset(string $charset)

Sets the default template charset.

Parameters

string $charset The default charset

at line 607
public string getCharset()

Gets the default template charset.

Return Value

string The default charset

at line 615
public initRuntime()

Initializes the runtime environment.

at line 631
public bool hasExtension(string $name)

Returns true if the given extension is registered.

Parameters

string $name The extension name

Return Value

bool Whether the extension is registered or not

at line 643
public Twig_ExtensionInterface getExtension(string $name)

Gets an extension by name.

Parameters

string $name The extension name

Return Value

Twig_ExtensionInterface A Twig_ExtensionInterface instance

at line 657
public addExtension(Twig_ExtensionInterface $extension)

Registers an extension.

Parameters

Twig_ExtensionInterface $extension A Twig_ExtensionInterface instance

at line 675
public removeExtension(string $name)

Removes an extension by name.

This method is deprecated and you should not use it.

Parameters

string $name The extension name

at line 689
public setExtensions(array $extensions)

Registers an array of extensions.

Parameters

array $extensions An array of extensions

at line 701
public array getExtensions()

Returns all registered extensions.

Return Value

array An array of extensions

at line 711
public addTokenParser(Twig_TokenParserInterface $parser)

Registers a Token Parser.

Parameters

Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance

at line 725
public Twig_TokenParserBrokerInterface getTokenParsers()

Gets the registered Token Parsers.

Return Value

Twig_TokenParserBrokerInterface A broker containing token parsers

at line 741
public Twig_TokenParserInterface[] getTags()

Gets registered tags.

Be warned that this method cannot return tags defined by Twig_TokenParserBrokerInterface classes.

Return Value

Twig_TokenParserInterface[] An array of Twig_TokenParserInterface instances

at line 758
public addNodeVisitor(Twig_NodeVisitorInterface $visitor)

Registers a Node Visitor.

Parameters

Twig_NodeVisitorInterface $visitor A Twig_NodeVisitorInterface instance

at line 772
public Twig_NodeVisitorInterface[] getNodeVisitors()

Gets the registered Node Visitors.

Return Value

Twig_NodeVisitorInterface[] An array of Twig_NodeVisitorInterface instances

at line 787
public addFilter(string|Twig_SimpleFilter $name, Twig_FilterInterface|Twig_SimpleFilter $filter = null)

Registers a Filter.

Parameters

string|Twig_SimpleFilter $name The filter name or a Twig_SimpleFilter instance
Twig_FilterInterface|Twig_SimpleFilter $filter A TwigFilterInterface instance or a TwigSimpleFilter instance

at line 815
public Twig_Filter|false getFilter(string $name)

Get a filter by name.

Subclasses may override this method and load filters differently; so no list of filters is available.

Parameters

string $name The filter name

Return Value

Twig_Filter|false A Twig_Filter instance or false if the filter does not exist

at line 847
public registerUndefinedFilterCallback($callable)

Parameters

$callable

at line 861
public Twig_FilterInterface[] getFilters()

Gets the registered Filters.

Be warned that this method cannot return filters defined with registerUndefinedFunctionCallback.

Return Value

Twig_FilterInterface[] An array of Twig_FilterInterface instances

See also

registerUndefinedFilterCallback

at line 876
public addTest(string|Twig_SimpleTest $name, Twig_TestInterface|Twig_SimpleTest $test = null)

Registers a Test.

Parameters

string|Twig_SimpleTest $name The test name or a Twig_SimpleTest instance
Twig_TestInterface|Twig_SimpleTest $test A TwigTestInterface instance or a TwigSimpleTest instance

at line 899
public Twig_TestInterface[] getTests()

Gets the registered Tests.

Return Value

Twig_TestInterface[] An array of Twig_TestInterface instances

at line 915
public Twig_Test|false getTest(string $name)

Gets a test by name.

Parameters

string $name The test name

Return Value

Twig_Test|false A Twig_Test instance or false if the test does not exist

at line 934
public addFunction(string|Twig_SimpleFunction $name, Twig_FunctionInterface|Twig_SimpleFunction $function = null)

Registers a Function.

Parameters

string|Twig_SimpleFunction $name The function name or a Twig_SimpleFunction instance
Twig_FunctionInterface|Twig_SimpleFunction $function A TwigFunctionInterface instance or a TwigSimpleFunction instance

at line 962
public Twig_Function|false getFunction(string $name)

Get a function by name.

Subclasses may override this method and load functions differently; so no list of functions is available.

Parameters

string $name function name

Return Value

Twig_Function|false A Twig_Function instance or false if the function does not exist

at line 994
public registerUndefinedFunctionCallback($callable)

Parameters

$callable

at line 1008
public Twig_FunctionInterface[] getFunctions()

Gets registered functions.

Be warned that this method cannot return functions defined with registerUndefinedFunctionCallback.

Return Value

Twig_FunctionInterface[] An array of Twig_FunctionInterface instances

See also

registerUndefinedFunctionCallback

at line 1026
public addGlobal(string $name, mixed $value)

Registers a Global.

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

Parameters

string $name The global name
mixed $value The global value

at line 1053
public array getGlobals()

Gets the registered Globals.

Return Value

array An array of globals

at line 1073
public array mergeGlobals(array $context)

Merges a context with the defined globals.

Parameters

array $context An array representing the context

Return Value

array The context merged with the globals

at line 1091
public array getUnaryOperators()

Gets the registered unary Operators.

Return Value

array An array of unary operators

at line 1105
public array getBinaryOperators()

Gets the registered binary Operators.

Return Value

array An array of binary operators

at line 1114
public computeAlternatives($name, $items)

Parameters

$name
$items