Constants

VERSION

VERSION = '2.6.2'

VERSION_ID

VERSION_ID = 20602

MAJOR_VERSION

MAJOR_VERSION = 2

MINOR_VERSION

MINOR_VERSION = 6

RELEASE_VERSION

RELEASE_VERSION = 2

EXTRA_VERSION

EXTRA_VERSION = ''

Properties

$charset

$charset : 

Type

$loader

$loader : 

Type

$debug

$debug : 

Type

$autoReload

$autoReload : 

Type

$cache

$cache : 

Type

$lexer

$lexer : 

Type

$parser

$parser : 

Type

$compiler

$compiler : 

Type

$baseTemplateClass

$baseTemplateClass : 

Type

$globals

$globals : 

Type

$resolvedGlobals

$resolvedGlobals : 

Type

$loadedTemplates

$loadedTemplates : 

Type

$strictVariables

$strictVariables : 

Type

$templateClassPrefix

$templateClassPrefix : 

Type

$originalCache

$originalCache : 

Type

$extensionSet

$extensionSet : 

Type

$runtimeLoaders

$runtimeLoaders : 

Type

$runtimes

$runtimes : 

Type

$optionsHash

$optionsHash : 

Type

$loading

$loading : 

Type

Methods

__construct()

__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

getBaseTemplateClass()

getBaseTemplateClass() : string

Gets the base template class for compiled templates.

Returns

string —

The base template class name

setBaseTemplateClass()

setBaseTemplateClass(string  $class) 

Sets the base template class for compiled templates.

Parameters

string $class

The base template class name

enableDebug()

enableDebug() 

Enables debugging mode.

disableDebug()

disableDebug() 

Disables debugging mode.

isDebug()

isDebug() : boolean

Checks if debug mode is enabled.

Returns

boolean —

true if debug mode is enabled, false otherwise

enableAutoReload()

enableAutoReload() 

Enables the auto_reload option.

disableAutoReload()

disableAutoReload() 

Disables the auto_reload option.

isAutoReload()

isAutoReload() : boolean

Checks if the auto_reload option is enabled.

Returns

boolean —

true if auto_reload is enabled, false otherwise

enableStrictVariables()

enableStrictVariables() 

Enables the strict_variables option.

disableStrictVariables()

disableStrictVariables() 

Disables the strict_variables option.

isStrictVariables()

isStrictVariables() : boolean

Checks if the strict_variables option is enabled.

Returns

boolean —

true if strict_variables is enabled, false otherwise

getCache()

getCache(boolean  $original = true) : \Twig_CacheInterface|string|false

Gets the current cache implementation.

Parameters

boolean $original

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

Returns

\Twig_CacheInterface|string|false —

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

setCache()

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

getTemplateClass()

getTemplateClass(string  $name, integer|null  $index = null) : string

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

integer|null $index

The index if it is an embedded template

Returns

string —

The template class name

render()

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

Renders a template.

Parameters

string $name

The template name

array $context

An array of parameters to pass to the template

Throws

\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

Returns

string —

The rendered template

display()

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

Throws

\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

load()

load(string|\Twig_TemplateWrapper|\Twig_Template  $name) : \Twig_TemplateWrapper

Loads a template.

Parameters

string|\Twig_TemplateWrapper|\Twig_Template $name

The template name

Throws

\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

Returns

\Twig_TemplateWrapper

createTemplate()

createTemplate(string  $template) : \Twig_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

Throws

\Twig_Error_Loader

When the template cannot be found

\Twig_Error_Syntax

When an error occurred during compilation

Returns

\Twig_Template —

A template instance representing the given template name

isTemplateFresh()

isTemplateFresh(string  $name, integer  $time) : boolean

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

integer $time

The last modification time of the cached template

Returns

boolean —

true if the template is fresh, false otherwise

resolveTemplate()

resolveTemplate(string|\Twig_Template|\Twig_TemplateWrapper|array  $names) : \Twig_Template|\Twig_TemplateWrapper

Tries to load a template consecutively from an array.

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

Parameters

string|\Twig_Template|\Twig_TemplateWrapper|array $names

A template or an array of templates to try consecutively

Throws

\Twig_Error_Loader

When none of the templates can be found

\Twig_Error_Syntax

When an error occurred during compilation

Returns

\Twig_Template|\Twig_TemplateWrapper

setLexer()

setLexer(\Twig_Lexer  $lexer) 

Parameters

\Twig_Lexer $lexer

tokenize()

tokenize(\Twig_Source  $source) : \Twig_TokenStream

Tokenizes a source code.

Parameters

\Twig_Source $source

Throws

\Twig_Error_Syntax

When the code is syntactically wrong

Returns

\Twig_TokenStream

setParser()

setParser(\Twig_Parser  $parser) 

Parameters

\Twig_Parser $parser

parse()

parse(\Twig_TokenStream  $stream) : \Twig_Node_Module

Converts a token stream to a node tree.

Parameters

\Twig_TokenStream $stream

Throws

\Twig_Error_Syntax

When the token stream is syntactically or semantically wrong

Returns

\Twig_Node_Module

setCompiler()

setCompiler(\Twig_Compiler  $compiler) 

Parameters

\Twig_Compiler $compiler

compile()

compile(\Twig_Node  $node) : string

Compiles a node and returns the PHP code.

Parameters

\Twig_Node $node

Returns

string —

The compiled PHP source code

compileSource()

compileSource(\Twig_Source  $source) : string

Compiles a template source code.

Parameters

\Twig_Source $source

Throws

\Twig_Error_Syntax

When there was an error during tokenizing, parsing or compiling

Returns

string —

The compiled PHP source code

getLoader()

getLoader() : \Twig_LoaderInterface

Gets the Loader instance.

Returns

\Twig_LoaderInterface

setCharset()

setCharset(string  $charset) 

Sets the default template charset.

Parameters

string $charset

The default charset

getCharset()

getCharset() : string

Gets the default template charset.

Returns

string —

The default charset

hasExtension()

hasExtension(string  $class) : boolean

Returns true if the given extension is registered.

Parameters

string $class

The extension class name

Returns

boolean —

Whether the extension is registered or not

addRuntimeLoader()

addRuntimeLoader(\Twig_RuntimeLoaderInterface  $loader) 

Adds a runtime loader.

Parameters

\Twig_RuntimeLoaderInterface $loader

getExtension()

getExtension(string  $class) : \Twig_ExtensionInterface

Gets an extension by class name.

Parameters

string $class

The extension class name

Returns

\Twig_ExtensionInterface

getRuntime()

getRuntime(string  $class) : object

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

Parameters

string $class

A runtime class name

Throws

\Twig_Error_Runtime

When the template cannot be found

Returns

object —

The runtime implementation

setExtensions()

setExtensions(array  $extensions) 

Registers an array of extensions.

Parameters

array $extensions

An array of extensions

getExtensions()

getExtensions() : array<mixed,\Twig_ExtensionInterface>

Returns all registered extensions.

Returns

array<mixed,\Twig_ExtensionInterface> —

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

addFilter()

addFilter(\Twig_Filter  $filter) 

Parameters

\Twig_Filter $filter

registerUndefinedFilterCallback()

registerUndefinedFilterCallback(callable  $callable) 

Parameters

callable $callable

addTest()

addTest(\Twig_Test  $test) 

Registers a Test.

Parameters

\Twig_Test $test

A Twig_Test instance

addFunction()

addFunction(\Twig_Function  $function) 

Parameters

\Twig_Function $function

registerUndefinedFunctionCallback()

registerUndefinedFunctionCallback(callable  $callable) 

Parameters

callable $callable

addGlobal()

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

mergeGlobals()

mergeGlobals(array  $context) : array

Merges a context with the defined globals.

Parameters

array $context

An array representing the context

Returns

array —

The context merged with the globals

updateOptionsHash()

updateOptionsHash()