Environment
in package
Stores the Twig configuration and renders templates.
Tags
Table of Contents
- EXTRA_VERSION = ''
- MAJOR_VERSION = 2
- MINOR_VERSION = 16
- RELEASE_VERSION = 0
- VERSION = '2.16.0'
- VERSION_ID = 21600
- $autoReload : mixed
- $baseTemplateClass : mixed
- $cache : mixed
- $charset : mixed
- $compiler : mixed
- $debug : mixed
- $extensionSet : mixed
- $globals : mixed
- $lexer : mixed
- $loadedTemplates : mixed
- $loader : mixed
- $optionsHash : mixed
- $originalCache : mixed
- $parser : mixed
- $resolvedGlobals : mixed
- $runtimeLoaders : mixed
- $runtimes : mixed
- $strictVariables : mixed
- $templateClassPrefix : mixed
- __construct() : mixed
- Constructor.
- addExtension() : mixed
- addFilter() : mixed
- addFunction() : mixed
- addGlobal() : mixed
- Registers a Global.
- addNodeVisitor() : mixed
- addRuntimeLoader() : mixed
- Adds a runtime loader.
- addTest() : mixed
- addTokenParser() : mixed
- compile() : string
- Compiles a node and returns the PHP code.
- compileSource() : string
- Compiles a template source code.
- createTemplate() : TemplateWrapper
- Creates a template from source.
- disableAutoReload() : mixed
- Disables the auto_reload option.
- disableDebug() : mixed
- Disables debugging mode.
- disableStrictVariables() : mixed
- Disables the strict_variables option.
- display() : mixed
- Displays a template.
- enableAutoReload() : mixed
- Enables the auto_reload option.
- enableDebug() : mixed
- Enables debugging mode.
- enableStrictVariables() : mixed
- Enables the strict_variables option.
- getBaseTemplateClass() : string
- Gets the base template class for compiled templates.
- getCache() : CacheInterface|string|false
- Gets the current cache implementation.
- getCharset() : string
- Gets the default template charset.
- getExtension() : ExtensionInterface
- Gets an extension by class name.
- getExtensions() : array<string|int, ExtensionInterface>
- Returns all registered extensions.
- getLoader() : LoaderInterface
- Gets the Loader instance.
- getRuntime() : object
- Returns the runtime implementation of a Twig element (filter/function/test).
- hasExtension() : bool
- Returns true if the given extension is registered.
- isAutoReload() : bool
- Checks if the auto_reload option is enabled.
- isDebug() : bool
- Checks if debug mode is enabled.
- isStrictVariables() : bool
- Checks if the strict_variables option is enabled.
- isTemplateFresh() : bool
- Returns true if the template is still fresh.
- load() : TemplateWrapper
- Loads a template.
- mergeGlobals() : array<string|int, mixed>
- Merges a context with the defined globals.
- parse() : ModuleNode
- Converts a token stream to a node tree.
- registerUndefinedFilterCallback() : mixed
- registerUndefinedFunctionCallback() : mixed
- render() : string
- Renders a template.
- resolveTemplate() : TemplateWrapper|Template
- Tries to load a template consecutively from an array.
- setBaseTemplateClass() : mixed
- Sets the base template class for compiled templates.
- setCache() : mixed
- Sets the current cache implementation.
- setCharset() : mixed
- Sets the default template charset.
- setCompiler() : mixed
- setExtensions() : mixed
- Registers an array of extensions.
- setLexer() : mixed
- setLoader() : mixed
- setParser() : mixed
- tokenize() : TokenStream
- Tokenizes a source code.
- updateOptionsHash() : mixed
Constants
EXTRA_VERSION
public
mixed
EXTRA_VERSION
= ''
MAJOR_VERSION
public
mixed
MAJOR_VERSION
= 2
MINOR_VERSION
public
mixed
MINOR_VERSION
= 16
RELEASE_VERSION
public
mixed
RELEASE_VERSION
= 0
VERSION
public
mixed
VERSION
= '2.16.0'
VERSION_ID
public
mixed
VERSION_ID
= 21600
Properties
$autoReload
private
mixed
$autoReload
$baseTemplateClass
private
mixed
$baseTemplateClass
$cache
private
mixed
$cache
$charset
private
mixed
$charset
$compiler
private
mixed
$compiler
$debug
private
mixed
$debug
$extensionSet
private
mixed
$extensionSet
$globals
private
mixed
$globals
= []
$lexer
private
mixed
$lexer
$loadedTemplates
private
mixed
$loadedTemplates
$loader
private
mixed
$loader
$optionsHash
private
mixed
$optionsHash
$originalCache
private
mixed
$originalCache
$parser
private
mixed
$parser
$resolvedGlobals
private
mixed
$resolvedGlobals
$runtimeLoaders
private
mixed
$runtimeLoaders
= []
$runtimes
private
mixed
$runtimes
= []
$strictVariables
private
mixed
$strictVariables
$templateClassPrefix
private
mixed
$templateClassPrefix
= '__TwigTemplate_'
Methods
__construct()
Constructor.
public
__construct(LoaderInterface $loader[, mixed $options = [] ]) : mixed
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\CacheInterface 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
- $loader : LoaderInterface
- $options : mixed = []
Return values
mixed —addExtension()
public
addExtension(ExtensionInterface $extension) : mixed
Parameters
- $extension : ExtensionInterface
Return values
mixed —addFilter()
public
addFilter(TwigFilter $filter) : mixed
Parameters
- $filter : TwigFilter
Return values
mixed —addFunction()
public
addFunction(TwigFunction $function) : mixed
Parameters
- $function : TwigFunction
Return values
mixed —addGlobal()
Registers a Global.
public
addGlobal(string $name, mixed $value) : mixed
New globals can be added before compiling or rendering a template; but after, you can only update existing globals.
Parameters
- $name : string
-
The global name
- $value : mixed
-
The global value
Return values
mixed —addNodeVisitor()
public
addNodeVisitor(NodeVisitorInterface $visitor) : mixed
Parameters
- $visitor : NodeVisitorInterface
Return values
mixed —addRuntimeLoader()
Adds a runtime loader.
public
addRuntimeLoader(RuntimeLoaderInterface $loader) : mixed
Parameters
- $loader : RuntimeLoaderInterface
Return values
mixed —addTest()
public
addTest(TwigTest $test) : mixed
Parameters
- $test : TwigTest
Return values
mixed —addTokenParser()
public
addTokenParser(TokenParserInterface $parser) : mixed
Parameters
- $parser : TokenParserInterface
Return values
mixed —compile()
Compiles a node and returns the PHP code.
public
compile(Node $node) : string
Parameters
- $node : Node
Return values
string —The compiled PHP source code
compileSource()
Compiles a template source code.
public
compileSource(Source $source) : string
Parameters
- $source : Source
Tags
Return values
string —The compiled PHP source code
createTemplate()
Creates a template from source.
public
createTemplate(string $template[, string $name = null ]) : TemplateWrapper
This method should not be used as a generic way to load templates.
Parameters
- $template : string
-
The template source
- $name : string = null
-
An optional name of the template to be used in error messages
Tags
Return values
TemplateWrapper —A template instance representing the given template name
disableAutoReload()
Disables the auto_reload option.
public
disableAutoReload() : mixed
Return values
mixed —disableDebug()
Disables debugging mode.
public
disableDebug() : mixed
Return values
mixed —disableStrictVariables()
Disables the strict_variables option.
public
disableStrictVariables() : mixed
Return values
mixed —display()
Displays a template.
public
display(string|TemplateWrapper $name[, array<string|int, mixed> $context = [] ]) : mixed
Parameters
- $name : string|TemplateWrapper
-
The template name
- $context : array<string|int, mixed> = []
-
An array of parameters to pass to the template
Tags
Return values
mixed —enableAutoReload()
Enables the auto_reload option.
public
enableAutoReload() : mixed
Return values
mixed —enableDebug()
Enables debugging mode.
public
enableDebug() : mixed
Return values
mixed —enableStrictVariables()
Enables the strict_variables option.
public
enableStrictVariables() : mixed
Return values
mixed —getBaseTemplateClass()
Gets the base template class for compiled templates.
public
getBaseTemplateClass() : string
Return values
string —The base template class name
getCache()
Gets the current cache implementation.
public
getCache([bool $original = true ]) : CacheInterface|string|false
Parameters
- $original : bool = true
-
Whether to return the original cache option or the real cache instance
Return values
CacheInterface|string|false —A Twig\Cache\CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache
getCharset()
Gets the default template charset.
public
getCharset() : string
Return values
string —The default charset
getExtension()
Gets an extension by class name.
public
getExtension(string $class) : ExtensionInterface
Parameters
- $class : string
-
The extension class name
Return values
ExtensionInterface —getExtensions()
Returns all registered extensions.
public
getExtensions() : array<string|int, ExtensionInterface>
Return values
array<string|int, ExtensionInterface> —An array of extensions (keys are for internal usage only and should not be relied on)
getLoader()
Gets the Loader instance.
public
getLoader() : LoaderInterface
Return values
LoaderInterface —getRuntime()
Returns the runtime implementation of a Twig element (filter/function/test).
public
getRuntime(string $class) : object
Parameters
- $class : string
-
A runtime class name
Tags
Return values
object —The runtime implementation
hasExtension()
Returns true if the given extension is registered.
public
hasExtension(string $class) : bool
Parameters
- $class : string
-
The extension class name
Return values
bool —Whether the extension is registered or not
isAutoReload()
Checks if the auto_reload option is enabled.
public
isAutoReload() : bool
Return values
bool —true if auto_reload is enabled, false otherwise
isDebug()
Checks if debug mode is enabled.
public
isDebug() : bool
Return values
bool —true if debug mode is enabled, false otherwise
isStrictVariables()
Checks if the strict_variables option is enabled.
public
isStrictVariables() : bool
Return values
bool —true if strict_variables is enabled, false otherwise
isTemplateFresh()
Returns true if the template is still fresh.
public
isTemplateFresh(string $name, int $time) : bool
Besides checking the loader for freshness information, this method also checks if the enabled extensions have not changed.
Parameters
- $name : string
-
The template name
- $time : int
-
The last modification time of the cached template
Return values
bool —true if the template is fresh, false otherwise
load()
Loads a template.
public
load(string|TemplateWrapper $name) : TemplateWrapper
Parameters
- $name : string|TemplateWrapper
-
The template name
Tags
Return values
TemplateWrapper —mergeGlobals()
Merges a context with the defined globals.
public
mergeGlobals(array<string|int, mixed> $context) : array<string|int, mixed>
Parameters
- $context : array<string|int, mixed>
-
An array representing the context
Return values
array<string|int, mixed> —The context merged with the globals
parse()
Converts a token stream to a node tree.
public
parse(TokenStream $stream) : ModuleNode
Parameters
- $stream : TokenStream
Tags
Return values
ModuleNode —registerUndefinedFilterCallback()
public
registerUndefinedFilterCallback(callable $callable) : mixed
Parameters
- $callable : callable
Return values
mixed —registerUndefinedFunctionCallback()
public
registerUndefinedFunctionCallback(callable $callable) : mixed
Parameters
- $callable : callable
Return values
mixed —render()
Renders a template.
public
render(string|TemplateWrapper $name[, array<string|int, mixed> $context = [] ]) : string
Parameters
- $name : string|TemplateWrapper
-
The template name
- $context : array<string|int, mixed> = []
-
An array of parameters to pass to the template
Tags
Return values
string —The rendered template
resolveTemplate()
Tries to load a template consecutively from an array.
public
resolveTemplate(string|TemplateWrapper|array<string|int, mixed> $names) : TemplateWrapper|Template
Similar to load() but it also accepts instances of \Twig\Template and \Twig\TemplateWrapper, and an array of templates where each is tried to be loaded.
Parameters
- $names : string|TemplateWrapper|array<string|int, mixed>
-
A template or an array of templates to try consecutively
Tags
Return values
TemplateWrapper|Template —setBaseTemplateClass()
Sets the base template class for compiled templates.
public
setBaseTemplateClass(string $class) : mixed
Parameters
- $class : string
-
The base template class name
Return values
mixed —setCache()
Sets the current cache implementation.
public
setCache(CacheInterface|string|false $cache) : mixed
Parameters
- $cache : CacheInterface|string|false
-
A Twig\Cache\CacheInterface implementation, an absolute path to the compiled templates, or false to disable cache
Return values
mixed —setCharset()
Sets the default template charset.
public
setCharset(string $charset) : mixed
Parameters
- $charset : string
-
The default charset
Return values
mixed —setCompiler()
public
setCompiler(Compiler $compiler) : mixed
Parameters
- $compiler : Compiler
Return values
mixed —setExtensions()
Registers an array of extensions.
public
setExtensions(array<string|int, mixed> $extensions) : mixed
Parameters
- $extensions : array<string|int, mixed>
-
An array of extensions
Return values
mixed —setLexer()
public
setLexer(Lexer $lexer) : mixed
Parameters
- $lexer : Lexer
Return values
mixed —setLoader()
public
setLoader(LoaderInterface $loader) : mixed
Parameters
- $loader : LoaderInterface
Return values
mixed —setParser()
public
setParser(Parser $parser) : mixed
Parameters
- $parser : Parser
Return values
mixed —tokenize()
Tokenizes a source code.
public
tokenize(Source $source) : TokenStream
Parameters
- $source : Source
Tags
Return values
TokenStream —updateOptionsHash()
private
updateOptionsHash() : mixed