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.

Boolean isDebug()

Checks if debug mode is enabled.

enableAutoReload()

Enables the auto_reload option.

disableAutoReload()

Disables the auto_reload option.

Boolean isAutoReload()

Checks if the auto_reload option is enabled.

enableStrictVariables()

Enables the strict_variables option.

disableStrictVariables()

Disables the strict_variables option.

Boolean 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 getCacheFilename(string $name)

Gets the cache filename for a given template.

string getTemplateClass(string $name, integer $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, integer $index = null)

Loads a template by name.

Boolean isTemplateFresh(string $name, timestamp $time)

Returns true if the template is still fresh.

resolveTemplate($names)

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 $tokens)

Parses a token stream.

Twig_CompilerInterface getCompiler()

Gets the Compiler instance.

setCompiler(Twig_CompilerInterface $compiler)

Sets the Compiler instance.

string compile(Twig_NodeInterface $node)

Compiles a Node.

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.

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

Checks if debug mode is enabled.

Return Value

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

Checks if the auto_reload option is enabled.

Return Value

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

Checks if the strict_variables option is enabled.

Return Value

Boolean 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 getCacheFilename(string $name)

Gets the cache filename for a given template.

Parameters

string $name The template name

Return Value

string The cache file name

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

Gets the template class associated with the given string.

Parameters

string $name The name for which to calculate the template class name
integer $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 286
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

at line 297
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

at line 310
public Twig_TemplateInterface loadTemplate(string $name, integer $index = null)

Loads a template by name.

Parameters

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

Return Value

Twig_TemplateInterface A template instance representing the given template name

at line 349
public Boolean 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

Boolean true if the template is fresh, false otherwise

at line 361
public resolveTemplate($names)

Parameters

$names

at line 388
public clearTemplateCache()

Clears the internal template cache.

at line 396
public clearCacheFiles()

Clears the template cache files on the filesystem.

at line 414
public Twig_LexerInterface getLexer()

Gets the Lexer instance.

Return Value

Twig_LexerInterface A Twig_LexerInterface instance

at line 428
public setLexer(Twig_LexerInterface $lexer)

Sets the Lexer instance.

Parameters

Twig_LexerInterface $lexer A Twig_LexerInterface instance

at line 441
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

at line 451
public Twig_ParserInterface getParser()

Gets the Parser instance.

Return Value

Twig_ParserInterface A Twig_ParserInterface instance

at line 465
public setParser(Twig_ParserInterface $parser)

Sets the Parser instance.

Parameters

Twig_ParserInterface $parser A Twig_ParserInterface instance

at line 477
public Twig_Node_Module parse(Twig_TokenStream $tokens)

Parses a token stream.

Parameters

Twig_TokenStream $tokens A Twig_TokenStream instance

Return Value

Twig_Node_Module A Node tree

at line 487
public Twig_CompilerInterface getCompiler()

Gets the Compiler instance.

Return Value

Twig_CompilerInterface A Twig_CompilerInterface instance

at line 501
public setCompiler(Twig_CompilerInterface $compiler)

Sets the Compiler instance.

Parameters

Twig_CompilerInterface $compiler A Twig_CompilerInterface instance

at line 513
public string compile(Twig_NodeInterface $node)

Compiles a Node.

Parameters

Twig_NodeInterface $node A Twig_NodeInterface instance

Return Value

string The compiled PHP source code

at line 526
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

at line 543
public setLoader(Twig_LoaderInterface $loader)

Sets the Loader instance.

Parameters

Twig_LoaderInterface $loader A Twig_LoaderInterface instance

at line 553
public Twig_LoaderInterface getLoader()

Gets the Loader instance.

Return Value

Twig_LoaderInterface A Twig_LoaderInterface instance

at line 567
public setCharset(string $charset)

Sets the default template charset.

Parameters

string $charset The default charset

at line 577
public string getCharset()

Gets the default template charset.

Return Value

string The default charset

at line 585
public initRuntime()

Initializes the runtime environment.

at line 601
public Boolean hasExtension(string $name)

Returns true if the given extension is registered.

Parameters

string $name The extension name

Return Value

Boolean Whether the extension is registered or not

at line 613
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 627
public addExtension(Twig_ExtensionInterface $extension)

Registers an extension.

Parameters

Twig_ExtensionInterface $extension A Twig_ExtensionInterface instance

at line 645
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 659
public setExtensions(array $extensions)

Registers an array of extensions.

Parameters

array $extensions An array of extensions

at line 671
public array getExtensions()

Returns all registered extensions.

Return Value

array An array of extensions

at line 681
public addTokenParser(Twig_TokenParserInterface $parser)

Registers a Token Parser.

Parameters

Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance

at line 695
public Twig_TokenParserBrokerInterface getTokenParsers()

Gets the registered Token Parsers.

Return Value

Twig_TokenParserBrokerInterface A broker containing token parsers

at line 711
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 728
public addNodeVisitor(Twig_NodeVisitorInterface $visitor)

Registers a Node Visitor.

Parameters

Twig_NodeVisitorInterface $visitor A Twig_NodeVisitorInterface instance

at line 742
public Twig_NodeVisitorInterface[] getNodeVisitors()

Gets the registered Node Visitors.

Return Value

Twig_NodeVisitorInterface[] An array of Twig_NodeVisitorInterface instances

at line 757
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 785
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 817
public registerUndefinedFilterCallback($callable)

Parameters

$callable

at line 831
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 846
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 869
public Twig_TestInterface[] getTests()

Gets the registered Tests.

Return Value

Twig_TestInterface[] An array of Twig_TestInterface instances

at line 885
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 904
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 932
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 964
public registerUndefinedFunctionCallback($callable)

Parameters

$callable

at line 978
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 996
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 1023
public array getGlobals()

Gets the registered Globals.

Return Value

array An array of globals

at line 1043
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 1061
public array getUnaryOperators()

Gets the registered unary Operators.

Return Value

array An array of unary operators

at line 1075
public array getBinaryOperators()

Gets the registered binary Operators.

Return Value

array An array of binary operators

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

Parameters

$name
$items