| Methods |
public
|
__construct(LoaderInterface $loader, $options = [])
Constructor.
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).
-
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).
|
#
|
public
|
enableDebug()
Enables debugging mode.
|
#
|
public
|
disableDebug()
Disables debugging mode.
|
#
|
public
|
isDebug(): bool
Checks if debug mode is enabled.
Checks if debug mode is enabled.
Returns
true if debug mode is enabled, false otherwise
|
#
|
public
|
enableAutoReload()
Enables the auto_reload option.
Enables the auto_reload option.
|
#
|
public
|
disableAutoReload()
Disables the auto_reload option.
Disables the auto_reload option.
|
#
|
public
|
isAutoReload(): bool
Checks if the auto_reload option is enabled.
Checks if the auto_reload option is enabled.
Returns
true if auto_reload is enabled, false otherwise
|
#
|
public
|
enableStrictVariables()
Enables the strict_variables option.
Enables the strict_variables option.
|
#
|
public
|
disableStrictVariables()
Disables the strict_variables option.
Disables the strict_variables option.
|
#
|
public
|
isStrictVariables(): bool
Checks if the strict_variables option is enabled.
Checks if the strict_variables option is enabled.
Returns
true if strict_variables is enabled, false otherwise
|
#
|
public
|
getCache(bool $original = true): CacheInterface|string|false
Gets the current cache implementation.
Gets the current cache implementation.
Parameters
| $original |
Whether to return the original cache option or the real cache instance
|
Returns
A Twig\Cache\CacheInterface implementation,
an absolute path to the compiled templates,
or false to disable cache
|
#
|
public
|
setCache(CacheInterface|string|false $cache)
Sets the current cache implementation.
Sets the current cache implementation.
Parameters
| $cache |
A Twig\Cache\CacheInterface implementation,
an absolute path to the compiled templates,
or false to disable cache
|
|
#
|
public
|
render(string|TemplateWrapper $name, array $context = []): string
Renders a template.
|
#
|
public
|
display(string|TemplateWrapper $name, array $context = []): void
Displays a template.
|
#
|
public
|
load(string|TemplateWrapper $name): TemplateWrapper
Loads a template.
|
#
|
public
|
createTemplate(string $template, string $name = null): TemplateWrapper
Creates a template from source.
Creates a template from source.
This method should not be used as a generic way to load templates.
Parameters
| $template |
The template source
|
| $name |
An optional name of the template to be used in error messages
|
Throws
|
#
|
public
|
isTemplateFresh(string $name, int $time): bool
Returns true if the template is still fresh.
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
| $time |
The last modification time of the cached template
|
|
#
|
public
|
resolveTemplate(string|TemplateWrapper|array $names): TemplateWrapper
Tries to load a template consecutively from an array.
Tries to load a template consecutively from an array.
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 |
A template or an array of templates to try consecutively
|
Throws
|
#
|
public
|
setLexer(Lexer $lexer)
|
#
|
public
|
tokenize(Source $source): TokenStream
|
#
|
public
|
setParser(Parser $parser)
|
#
|
public
|
parse(TokenStream $stream): ModuleNode
Converts a token stream to a node tree.
Converts a token stream to a node tree.
Throws
|
#
|
public
|
setCompiler(Compiler $compiler)
|
#
|
public
|
compile(Node $node): string
Compiles a node and returns the PHP code.
Compiles a node and returns the PHP code.
|
#
|
public
|
compileSource(Source $source): string
Compiles a template source code.
Compiles a template source code.
Throws
|
#
|
public
|
setLoader(LoaderInterface $loader)
|
#
|
public
|
getLoader(): LoaderInterface
|
#
|
public
|
setCharset(string $charset)
|
#
|
public
|
getCharset(): string
|
#
|
public
|
hasExtension(string $class): bool
|
#
|
public
|
addRuntimeLoader(RuntimeLoaderInterface $loader)
|
#
|
public
|
getExtension<TExtension is ExtensionInterface>(class-string<TExtension> $class): TExtension
|
#
|
public
|
getRuntime<TRuntime is object>(class-string<TRuntime> $class): TRuntime
Returns the runtime implementation of a Twig element (filter/function/tag/test).
Returns the runtime implementation of a Twig element (filter/function/tag/test).
Parameters
| $class |
A runtime class name
|
Returns
The runtime implementation
Throws
|
#
|
public
|
addExtension(ExtensionInterface $extension)
|
#
|
public
|
setExtensions(ExtensionInterface[] $extensions)
Parameters
| $extensions |
An array of extensions
|
|
#
|
public
|
getExtensions(): ExtensionInterface[]
Returns
An array of extensions (keys are for internal usage only and should not be relied on)
|
#
|
public
|
addTokenParser(TokenParserInterface $parser)
|
#
|
public
|
registerUndefinedTokenParserCallback(callable $callable): void
|
#
|
public
|
addNodeVisitor(NodeVisitorInterface $visitor)
|
#
|
public
|
addFilter(TwigFilter $filter)
|
#
|
public
|
registerUndefinedFilterCallback(callable $callable): void
|
#
|
public
|
addTest(TwigTest $test)
|
#
|
public
|
addFunction(TwigFunction $function)
|
#
|
public
|
registerUndefinedFunctionCallback(callable $callable): void
|
#
|
public
|
addGlobal(string $name, mixed $value)
Registers a Global.
Registers a Global.
New globals can be added before compiling or rendering a template;
but after, you can only update existing globals.
Parameters
|
#
|
public
|
mergeGlobals(array $context): array
|
#
|
private
|
updateOptionsHash(): void
|
#
|