Class Twig_Environment

InheritanceTwig_Environment

Stores the Twig configuration.

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Constructor. Twig_Environment
addExtension() Registers an extension. Twig_Environment
addFilter() Registers a Filter. Twig_Environment
addFunction() Registers a Function. Twig_Environment
addGlobal() Registers a Global. Twig_Environment
addNodeVisitor() Registers a Node Visitor. Twig_Environment
addTest() Registers a Test. Twig_Environment
addTokenParser() Registers a Token Parser. Twig_Environment
clearCacheFiles() Clears the template cache files on the filesystem. Twig_Environment
clearTemplateCache() Clears the internal template cache. Twig_Environment
compile() Compiles a node and returns the PHP code. Twig_Environment
compileSource() Compiles a template source code. Twig_Environment
computeAlternatives() Twig_Environment
createTemplate() Creates a template from source. Twig_Environment
disableAutoReload() Disables the auto_reload option. Twig_Environment
disableDebug() Disables debugging mode. Twig_Environment
disableStrictVariables() Disables the strict_variables option. Twig_Environment
display() Displays a template. Twig_Environment
enableAutoReload() Enables the auto_reload option. Twig_Environment
enableDebug() Enables debugging mode. Twig_Environment
enableStrictVariables() Enables the strict_variables option. Twig_Environment
getBaseTemplateClass() Gets the base template class for compiled templates. Twig_Environment
getBinaryOperators() Gets the registered binary Operators. Twig_Environment
getCache() Gets the current cache implementation. Twig_Environment
getCacheFilename() Gets the cache filename for a given template. Twig_Environment
getCharset() Gets the default template charset. Twig_Environment
getCompiler() Gets the Compiler instance. Twig_Environment
getExtension() Gets an extension by name. Twig_Environment
getExtensions() Returns all registered extensions. Twig_Environment
getFilter() Get a filter by name. Twig_Environment
getFilters() Gets the registered Filters. Twig_Environment
getFunction() Get a function by name. Twig_Environment
getFunctions() Gets registered functions. Twig_Environment
getGlobals() Gets the registered Globals. Twig_Environment
getLexer() Gets the Lexer instance. Twig_Environment
getLoader() Gets the Loader instance. Twig_Environment
getNodeVisitors() Gets the registered Node Visitors. Twig_Environment
getParser() Gets the Parser instance. Twig_Environment
getTags() Gets registered tags. Twig_Environment
getTemplateClass() Gets the template class associated with the given string. Twig_Environment
getTemplateClassPrefix() Gets the template class prefix. Twig_Environment
getTest() Gets a test by name. Twig_Environment
getTests() Gets the registered Tests. Twig_Environment
getTokenParsers() Gets the registered Token Parsers. Twig_Environment
getUnaryOperators() Gets the registered unary Operators. Twig_Environment
hasExtension() Returns true if the given extension is registered. Twig_Environment
initRuntime() Initializes the runtime environment. Twig_Environment
isAutoReload() Checks if the auto_reload option is enabled. Twig_Environment
isDebug() Checks if debug mode is enabled. Twig_Environment
isStrictVariables() Checks if the strict_variables option is enabled. Twig_Environment
isTemplateFresh() Returns true if the template is still fresh. Twig_Environment
loadTemplate() Loads a template by name. Twig_Environment
mergeGlobals() Merges a context with the defined globals. Twig_Environment
parse() Converts a token stream to a node tree. Twig_Environment
registerUndefinedFilterCallback() Twig_Environment
registerUndefinedFunctionCallback() Twig_Environment
removeExtension() Removes an extension by name. Twig_Environment
render() Renders a template. Twig_Environment
resolveTemplate() Tries to load a template consecutively from an array. Twig_Environment
setBaseTemplateClass() Sets the base template class for compiled templates. Twig_Environment
setCache() Sets the current cache implementation. Twig_Environment
setCharset() Sets the default template charset. Twig_Environment
setCompiler() Sets the Compiler instance. Twig_Environment
setExtensions() Registers an array of extensions. Twig_Environment
setLexer() Sets the Lexer instance. Twig_Environment
setLoader() Sets the Loader instance. Twig_Environment
setParser() Sets the Parser instance. Twig_Environment
tokenize() Tokenizes a source code. Twig_Environment

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
VERSION '1.25.0' Twig_Environment

Property Details

$autoReload protected property
protected $autoReload null
$baseTemplateClass protected property
protected $baseTemplateClass null
$binaryOperators protected property
protected $binaryOperators null
$cache protected property
protected $cache null
$charset protected property
protected $charset null
$compiler protected property
protected $compiler null
$debug protected property
protected $debug null
$extensionInitialized protected property
protected $extensionInitialized false
$extensions protected property
protected $extensions null
$filterCallbacks protected property
protected $filterCallbacks = []
$filters protected property
protected $filters null
$functionCallbacks protected property
protected $functionCallbacks = []
$functions protected property
protected $functions null
$globals protected property
protected $globals null
$lexer protected property
protected $lexer null
$loadedTemplates protected property
protected $loadedTemplates null
$loader protected property
protected $loader null
$parser protected property
protected $parser null
$parsers protected property
protected $parsers null
$runtimeInitialized protected property
protected $runtimeInitialized false
$staging protected property
protected $staging null
$strictVariables protected property
protected $strictVariables null
$templateClassPrefix protected property
protected $templateClassPrefix '__TwigTemplate_'
$tests protected property
protected $tests null
$unaryOperators protected property
protected $unaryOperators null
$visitors protected property
protected $visitors null

Method Details

__construct() public method

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
              * true: equivalent to html
              * html, js: set the autoescaping to one of the supported strategies
              * filename: set the autoescaping strategy based on the template filename extension
              * PHP callback: a PHP callback that returns an escaping strategy based on the template "filename"
    
  • 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).
    
public void __construct ( Twig_LoaderInterface $loader null, $options = [] )
$loader Twig_LoaderInterface

A Twig_LoaderInterface instance

$options array

An array of options

addExtension() public method

Registers an extension.

public void addExtension ( Twig_ExtensionInterface $extension )
$extension Twig_ExtensionInterface

A Twig_ExtensionInterface instance

addFilter() public method

Registers a Filter.

public void addFilter ( $name, $filter null )
$name string|Twig_SimpleFilter

The filter name or a Twig_SimpleFilter instance

$filter Twig_FilterInterface|Twig_SimpleFilter

A Twig_FilterInterface instance or a Twig_SimpleFilter instance

addFunction() public method

Registers a Function.

public void addFunction ( $name, $function null )
$name string|Twig_SimpleFunction

The function name or a Twig_SimpleFunction instance

$function Twig_FunctionInterface|Twig_SimpleFunction

A Twig_FunctionInterface instance or a Twig_SimpleFunction instance

addGlobal() public method

Registers a Global.

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

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

The global name

$value mixed

The global value

addNodeVisitor() public method

Registers a Node Visitor.

public void addNodeVisitor ( Twig_NodeVisitorInterface $visitor )
$visitor Twig_NodeVisitorInterface

A Twig_NodeVisitorInterface instance

addTest() public method

Registers a Test.

public void addTest ( $name, $test null )
$name string|Twig_SimpleTest

The test name or a Twig_SimpleTest instance

$test Twig_TestInterface|Twig_SimpleTest

A Twig_TestInterface instance or a Twig_SimpleTest instance

addTokenParser() public method

Registers a Token Parser.

public void addTokenParser ( Twig_TokenParserInterface $parser )
$parser Twig_TokenParserInterface

A Twig_TokenParserInterface instance

clearCacheFiles() public method

Clears the template cache files on the filesystem.

public void clearCacheFiles ( )
clearTemplateCache() public method

Clears the internal template cache.

public void clearTemplateCache ( )
compile() public method

Compiles a node and returns the PHP code.

public string compile ( Twig_NodeInterface $node )
$node Twig_NodeInterface

A Twig_NodeInterface instance

return string

The compiled PHP source code

compileSource() public method

Compiles a template source code.

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

The template source code

$name string

The template name

return string

The compiled PHP source code

throws Twig_Error_Syntax

When there was an error during tokenizing, parsing or compiling

computeAlternatives() public method

public void computeAlternatives ( $name, $items )
$name
$items
createTemplate() public method

Creates a template from source.

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

public Twig_Template createTemplate ( $template )
$template string

The template name

return Twig_Template

A template instance representing the given template name

throws Twig_Error_Loader

When the template cannot be found

throws Twig_Error_Syntax

When an error occurred during compilation

disableAutoReload() public method

Disables the auto_reload option.

public void disableAutoReload ( )
disableDebug() public method

Disables debugging mode.

public void disableDebug ( )
disableStrictVariables() public method

Disables the strict_variables option.

public void disableStrictVariables ( )
display() public method

Displays a template.

public void display ( $name, array $context = [] )
$name string

The template name

$context array

An array of parameters to pass to the template

throws Twig_Error_Loader

When the template cannot be found

throws Twig_Error_Syntax

When an error occurred during compilation

throws Twig_Error_Runtime

When an error occurred during rendering

enableAutoReload() public method

Enables the auto_reload option.

public void enableAutoReload ( )
enableDebug() public method

Enables debugging mode.

public void enableDebug ( )
enableStrictVariables() public method

Enables the strict_variables option.

public void enableStrictVariables ( )
getBaseTemplateClass() public method

Gets the base template class for compiled templates.

public string getBaseTemplateClass ( )
return string

The base template class name

getBinaryOperators() public method

Gets the registered binary Operators.

public array getBinaryOperators ( )
return array

An array of binary operators

getCache() public method

Gets the current cache implementation.

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

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

return Twig_CacheInterface|string|false

A Twig_CacheInterface implementation,

                                     an absolute path to the compiled templates,
                                     or false to disable cache
getCacheFilename() public method

Gets the cache filename for a given template.

public string|false getCacheFilename ( $name )
$name string

The template name

return string|false

The cache file name or false when caching is disabled

getCharset() public method

Gets the default template charset.

public string getCharset ( )
return string

The default charset

getCompiler() public method

Gets the Compiler instance.

public Twig_CompilerInterface getCompiler ( )
return Twig_CompilerInterface

A Twig_CompilerInterface instance

getExtension() public method

Gets an extension by name.

public Twig_ExtensionInterface getExtension ( $name )
$name string

The extension name

return Twig_ExtensionInterface

A Twig_ExtensionInterface instance

getExtensions() public method

Returns all registered extensions.

public array getExtensions ( )
return array

An array of extensions

getFilter() public method

Get a filter by name.

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

public Twig_Filter|false getFilter ( $name )
$name string

The filter name

return Twig_Filter|false

A Twig_Filter instance or false if the filter does not exist

getFilters() public method

Gets the registered Filters.

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

See also registerUndefinedFilterCallback().

public Twig_FilterInterface[] getFilters ( )
return Twig_FilterInterface[]

An array of Twig_FilterInterface instances

getFunction() public method

Get a function by name.

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

public Twig_Function|false getFunction ( $name )
$name string

Function name

return Twig_Function|false

A Twig_Function instance or false if the function does not exist

getFunctions() public method

Gets registered functions.

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

See also registerUndefinedFunctionCallback().

public Twig_FunctionInterface[] getFunctions ( )
return Twig_FunctionInterface[]

An array of Twig_FunctionInterface instances

getGlobals() public method

Gets the registered Globals.

public array getGlobals ( )
return array

An array of globals

getLexer() public method

Gets the Lexer instance.

public Twig_LexerInterface getLexer ( )
return Twig_LexerInterface

A Twig_LexerInterface instance

getLoader() public method

Gets the Loader instance.

public Twig_LoaderInterface getLoader ( )
return Twig_LoaderInterface

A Twig_LoaderInterface instance

getNodeVisitors() public method

Gets the registered Node Visitors.

public Twig_NodeVisitorInterface[] getNodeVisitors ( )
return Twig_NodeVisitorInterface[]

An array of Twig_NodeVisitorInterface instances

getParser() public method

Gets the Parser instance.

public Twig_ParserInterface getParser ( )
return Twig_ParserInterface

A Twig_ParserInterface instance

getTags() public method

Gets registered tags.

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

public Twig_TokenParserInterface[] getTags ( )
return Twig_TokenParserInterface[]

An array of Twig_TokenParserInterface instances

getTemplateClass() public method

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

The name for which to calculate the template class name

$index int|null

The index if it is an embedded template

return string

The template class name

getTemplateClassPrefix() public method

Gets the template class prefix.

public string getTemplateClassPrefix ( )
return string

The template class prefix

getTest() public method

Gets a test by name.

public Twig_Test|false getTest ( $name )
$name string

The test name

return Twig_Test|false

A Twig_Test instance or false if the test does not exist

getTests() public method

Gets the registered Tests.

public Twig_TestInterface[] getTests ( )
return Twig_TestInterface[]

An array of Twig_TestInterface instances

getTokenParsers() public method

Gets the registered Token Parsers.

public Twig_TokenParserBrokerInterface getTokenParsers ( )
return Twig_TokenParserBrokerInterface

A broker containing token parsers

getUnaryOperators() public method

Gets the registered unary Operators.

public array getUnaryOperators ( )
return array

An array of unary operators

hasExtension() public method

Returns true if the given extension is registered.

public bool hasExtension ( $name )
$name string

The extension name

return bool

Whether the extension is registered or not

initExtension() protected method

protected void initExtension ( Twig_ExtensionInterface $extension )
$extension
initExtensions() protected method

protected void initExtensions ( )
initGlobals() protected method

protected void initGlobals ( )
initRuntime() public method

Initializes the runtime environment.

public void initRuntime ( )
isAutoReload() public method

Checks if the auto_reload option is enabled.

public bool isAutoReload ( )
return bool

True if auto_reload is enabled, false otherwise

isDebug() public method

Checks if debug mode is enabled.

public bool isDebug ( )
return bool

True if debug mode is enabled, false otherwise

isStrictVariables() public method

Checks if the strict_variables option is enabled.

public bool isStrictVariables ( )
return bool

True if strict_variables is enabled, false otherwise

isTemplateFresh() public method

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.

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

The template name

$time int

The last modification time of the cached template

return bool

True if the template is fresh, false otherwise

loadTemplate() public method

Loads a template by name.

public Twig_TemplateInterface loadTemplate ( $name, $index null )
$name string

The template name

$index int

The index if it is an embedded template

return Twig_TemplateInterface

A template instance representing the given template name

throws Twig_Error_Loader

When the template cannot be found

throws Twig_Error_Syntax

When an error occurred during compilation

mergeGlobals() public method

Merges a context with the defined globals.

public array mergeGlobals ( array $context )
$context array

An array representing the context

return array

The context merged with the globals

parse() public method

Converts a token stream to a node tree.

public Twig_Node_Module parse ( Twig_TokenStream $stream )
$stream Twig_TokenStream

A token stream instance

return Twig_Node_Module

A node tree

throws Twig_Error_Syntax

When the token stream is syntactically or semantically wrong

registerUndefinedFilterCallback() public method

public void registerUndefinedFilterCallback ( $callable )
$callable
registerUndefinedFunctionCallback() public method

public void registerUndefinedFunctionCallback ( $callable )
$callable
removeExtension() public method

Removes an extension by name.

This method is deprecated and you should not use it.

public void removeExtension ( $name )
$name string

The extension name

render() public method

Renders a template.

public string render ( $name, array $context = [] )
$name string

The template name

$context array

An array of parameters to pass to the template

return string

The rendered template

throws Twig_Error_Loader

When the template cannot be found

throws Twig_Error_Syntax

When an error occurred during compilation

throws Twig_Error_Runtime

When an error occurred during rendering

resolveTemplate() public method

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.

public Twig_Template resolveTemplate ( $names )
$names string|Twig_Template|array

A template or an array of templates to try consecutively

throws Twig_Error_Loader

When none of the templates can be found

throws Twig_Error_Syntax

When an error occurred during compilation

setBaseTemplateClass() public method

Sets the base template class for compiled templates.

public void setBaseTemplateClass ( $class )
$class string

The base template class name

setCache() public method

Sets the current cache implementation.

public void setCache ( $cache )
$cache Twig_CacheInterface|string|false

A Twig_CacheInterface implementation,

                                           an absolute path to the compiled templates,
                                           or false to disable cache
setCharset() public method

Sets the default template charset.

public void setCharset ( $charset )
$charset string

The default charset

setCompiler() public method

Sets the Compiler instance.

public void setCompiler ( Twig_CompilerInterface $compiler )
$compiler Twig_CompilerInterface

A Twig_CompilerInterface instance

setExtensions() public method

Registers an array of extensions.

public void setExtensions ( array $extensions )
$extensions array

An array of extensions

setLexer() public method

Sets the Lexer instance.

public void setLexer ( Twig_LexerInterface $lexer )
$lexer Twig_LexerInterface

A Twig_LexerInterface instance

setLoader() public method

Sets the Loader instance.

public void setLoader ( Twig_LoaderInterface $loader )
$loader Twig_LoaderInterface

A Twig_LoaderInterface instance

setParser() public method

Sets the Parser instance.

public void setParser ( Twig_ParserInterface $parser )
$parser Twig_ParserInterface

A Twig_ParserInterface instance

tokenize() public method

Tokenizes a source code.

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

The template source code

$name string

The template name

return Twig_TokenStream

A Twig_TokenStream instance

throws Twig_Error_Syntax

When the code is syntactically wrong

writeCacheFile() protected method

protected void writeCacheFile ( $file, $content )
$file
$content