class Twig_Environment

Stores the Twig configuration.

Constants

VERSION

VERSION_ID

MAJOR_VERSION

MINOR_VERSION

RELEASE_VERSION

EXTRA_VERSION

Methods

__construct(Twig_LoaderInterface $loader, 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.

Twig_CacheInterface|string|false
getCache(bool $original = true)

Gets the current cache implementation.

setCache(Twig_CacheInterface|string|false $cache)

Sets the current cache implementation.

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

Gets the template class associated with the given string.

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

Renders a template.

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

Displays a template.

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

Loads a template internal representation.

createTemplate(string $template)

Creates a template from source.

bool
isTemplateFresh(string $name, int $time)

Returns true if the template is still fresh.

resolveTemplate(string|Twig_Template|array $names)

Tries to load a template consecutively from an array.

setLexer(Twig_Lexer $lexer)

No description

tokenize(Twig_Source $source)

Tokenizes a source code.

setParser(Twig_Parser $parser)

No description

parse(Twig_TokenStream $stream)

Converts a token stream to a node tree.

setCompiler(Twig_Compiler $compiler)

No description

string
compile(Twig_Node $node)

Compiles a node and returns the PHP code.

string
compileSource(Twig_Source $source)

Compiles a template source code.

setLoader(Twig_LoaderInterface $loader)

No description

getLoader()

Gets the Loader instance.

setCharset(string $charset)

Sets the default template charset.

string
getCharset()

Gets the default template charset.

bool
hasExtension(string $class)

Returns true if the given extension is registered.

addRuntimeLoader(Twig_RuntimeLoaderInterface $loader)

Adds a runtime loader.

getExtension(string $class)

Gets an extension by class name.

object
getRuntime(string $class)

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

addExtension(Twig_ExtensionInterface $extension)

No description

setExtensions(array $extensions)

Registers an array of extensions.

getExtensions()

Returns all registered extensions.

addTokenParser(Twig_TokenParserInterface $parser)

No description

getTokenParsers()

Gets the registered Token Parsers.

getTags()

Gets registered tags.

addNodeVisitor(Twig_NodeVisitorInterface $visitor)

No description

getNodeVisitors()

Gets the registered Node Visitors.

addFilter(Twig_Filter $filter)

No description

Twig_Filter|false
getFilter(string $name)

Get a filter by name.

registerUndefinedFilterCallback(callable $callable)

No description

getFilters()

Gets the registered Filters.

addTest(Twig_Test $test)

Registers a Test.

getTests()

Gets the registered Tests.

Twig_Test|false
getTest(string $name)

Gets a test by name.

addFunction(Twig_Function $function)

No description

getFunction(string $name)

Get a function by name.

registerUndefinedFunctionCallback(callable $callable)

No description

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.

Details

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

Constructor.

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

Twig_LoaderInterface $loader
array $options An array of options

at line 117
string getBaseTemplateClass()

Gets the base template class for compiled templates.

Return Value

string The base template class name

at line 127
setBaseTemplateClass(string $class)

Sets the base template class for compiled templates.

Parameters

string $class The base template class name

at line 136
enableDebug()

Enables debugging mode.

at line 145
disableDebug()

Disables debugging mode.

at line 156
bool isDebug()

Checks if debug mode is enabled.

Return Value

bool true if debug mode is enabled, false otherwise

at line 164
enableAutoReload()

Enables the auto_reload option.

at line 172
disableAutoReload()

Disables the auto_reload option.

at line 182
bool isAutoReload()

Checks if the auto_reload option is enabled.

Return Value

bool true if auto_reload is enabled, false otherwise

at line 190
enableStrictVariables()

Enables the strict_variables option.

at line 199
disableStrictVariables()

Disables the strict_variables option.

at line 210
bool isStrictVariables()

Checks if the strict_variables option is enabled.

Return Value

bool true if strict_variables is enabled, false otherwise

at line 224
Twig_CacheInterface|string|false getCache(bool $original = true)

Gets the current cache implementation.

Parameters

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

Return Value

Twig_CacheInterface|string|false A Twig_CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache

at line 236
setCache(Twig_CacheInterface|string|false $cache)

Sets the current cache implementation.

Parameters

Twig_CacheInterface|string|false $cache A Twig_CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache

at line 268
string getTemplateClass(string $name, int|null $index = null)

Gets the template class associated with the given string.

The generated template class is based on the following parameters:

  • The cache key for the given template;
  • The currently enabled extensions;
  • Whether the Twig C extension is available or not;
  • PHP version;
  • Twig version;
  • Options with what environment was created.

Parameters

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

Return Value

string The template class name

at line 287
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 302
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 314
Twig_TemplateWrapper load(string|Twig_TemplateWrapper|Twig_Template $name)

Loads a template.

Parameters

string|Twig_TemplateWrapper|Twig_Template $name The template name

Return Value

Twig_TemplateWrapper

at line 344
Twig_Template loadTemplate(string $name, int $index = null)

Loads a template internal representation.

This method is for internal use only and should never be called directly.

Parameters

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

Return Value

Twig_Template A template instance representing the given template name

Exceptions

Twig_Error_Loader When the template cannot be found
Twig_Error_Runtime When a previously generated cache is corrupted
Twig_Error_Syntax When an error occurred during compilation

at line 413
Twig_Template createTemplate(string $template)

Creates a template from source.

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

Parameters

string $template The template name

Return Value

Twig_Template 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 444
bool isTemplateFresh(string $name, int $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
int $time The last modification time of the cached template

Return Value

bool true if the template is fresh, false otherwise

at line 462
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_Template 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 486
setLexer(Twig_Lexer $lexer)

Parameters

Twig_Lexer $lexer

at line 498
Twig_TokenStream tokenize(Twig_Source $source)

Tokenizes a source code.

Parameters

Twig_Source $source

Return Value

Twig_TokenStream

Exceptions

Twig_Error_Syntax When the code is syntactically wrong

at line 507
setParser(Twig_Parser $parser)

Parameters

Twig_Parser $parser

at line 519
Twig_Node_Module parse(Twig_TokenStream $stream)

Converts a token stream to a node tree.

Parameters

Twig_TokenStream $stream

Return Value

Twig_Node_Module

Exceptions

Twig_Error_Syntax When the token stream is syntactically or semantically wrong

at line 528
setCompiler(Twig_Compiler $compiler)

Parameters

Twig_Compiler $compiler

at line 538
string compile(Twig_Node $node)

Compiles a node and returns the PHP code.

Parameters

Twig_Node $node

Return Value

string The compiled PHP source code

at line 554
string compileSource(Twig_Source $source)

Compiles a template source code.

Parameters

Twig_Source $source

Return Value

string The compiled PHP source code

Exceptions

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

at line 566
setLoader(Twig_LoaderInterface $loader)

Parameters

Twig_LoaderInterface $loader

at line 576
Twig_LoaderInterface getLoader()

Gets the Loader instance.

Return Value

Twig_LoaderInterface

at line 586
setCharset(string $charset)

Sets the default template charset.

Parameters

string $charset The default charset

at line 601
string getCharset()

Gets the default template charset.

Return Value

string The default charset

at line 613
bool hasExtension(string $class)

Returns true if the given extension is registered.

Parameters

string $class The extension class name

Return Value

bool Whether the extension is registered or not

at line 621
addRuntimeLoader(Twig_RuntimeLoaderInterface $loader)

Adds a runtime loader.

Parameters

Twig_RuntimeLoaderInterface $loader

at line 633
Twig_ExtensionInterface getExtension(string $class)

Gets an extension by class name.

Parameters

string $class The extension class name

Return Value

Twig_ExtensionInterface

at line 647
object getRuntime(string $class)

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

Parameters

string $class A runtime class name

Return Value

object The runtime implementation

Exceptions

Twig_Error_Runtime When the template cannot be found

at line 662
addExtension(Twig_ExtensionInterface $extension)

Parameters

Twig_ExtensionInterface $extension

at line 673
setExtensions(array $extensions)

Registers an array of extensions.

Parameters

array $extensions An array of extensions

at line 683
Twig_ExtensionInterface[] getExtensions()

Returns all registered extensions.

Return Value

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

at line 688
addTokenParser(Twig_TokenParserInterface $parser)

Parameters

Twig_TokenParserInterface $parser

at line 700
Twig_TokenParserInterface[] getTokenParsers()

Gets the registered Token Parsers.

Return Value

Twig_TokenParserInterface[]

at line 712
Twig_TokenParserInterface[] getTags()

Gets registered tags.

Return Value

Twig_TokenParserInterface[]

at line 722
addNodeVisitor(Twig_NodeVisitorInterface $visitor)

Parameters

Twig_NodeVisitorInterface $visitor

at line 734
Twig_NodeVisitorInterface[] getNodeVisitors()

Gets the registered Node Visitors.

Return Value

Twig_NodeVisitorInterface[]

at line 739
addFilter(Twig_Filter $filter)

Parameters

Twig_Filter $filter

at line 756
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 761
registerUndefinedFilterCallback(callable $callable)

Parameters

callable $callable

at line 777
Twig_Filter[] getFilters()

Gets the registered Filters.

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

Return Value

Twig_Filter[]

See also

registerUndefinedFilterCallback

at line 787
addTest(Twig_Test $test)

Registers a Test.

Parameters

Twig_Test $test A Twig_Test instance

at line 799
Twig_Test[] getTests()

Gets the registered Tests.

Return Value

Twig_Test[]

at line 813
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 818
addFunction(Twig_Function $function)

Parameters

Twig_Function $function

at line 835
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 840
registerUndefinedFunctionCallback(callable $callable)

Parameters

callable $callable

at line 856
Twig_Function[] getFunctions()

Gets registered functions.

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

Return Value

Twig_Function[]

See also

registerUndefinedFunctionCallback

at line 870
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 890
array getGlobals()

Gets the registered Globals.

Return Value

array An array of globals

at line 910
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 930
array getUnaryOperators()

Gets the registered unary Operators.

Return Value

array An array of unary operators

at line 942
array getBinaryOperators()

Gets the registered binary Operators.

Return Value

array An array of binary operators