Environment

Type
Class
Namespace
craft\web\twig
Inherits
craft\web\twig\Environment » Twig_Environment
Since
3.0

Base Twig template class.

View source

Public Methods

MethodDescription
__construct()Constructor.
addExtension()
addFilter()
addFunction()
addGlobal()Registers a Global.
addNodeVisitor()
addRuntimeLoader()Adds a runtime loader.
addTest()Registers a Test.
addTokenParser()
compile()Compiles a node and returns the PHP code.
compileSource()Compiles a template source code.
createTemplate()Creates a template from source.
disableAutoReload()Disables the auto_reload option.
disableDebug()Disables debugging mode.
disableStrictVariables()Disables the strict_variables option.
display()Displays a template.
enableAutoReload()Enables the auto_reload option.
enableDebug()Enables debugging mode.
enableStrictVariables()Enables the strict_variables option.
getBaseTemplateClass()Gets the base template class for compiled templates.
getBinaryOperators()Gets the registered binary Operators.
getCache()Gets the current cache implementation.
getCharset()Gets the default template charset.
getDefaultEscaperStrategy()Returns the default escaper strategy to use based on the template name.
getExtension()Gets an extension by class name.
getExtensions()Returns all registered extensions.
getFilter()Get a filter by name.
getFilters()Gets the registered Filters.
getFunction()Get a function by name.
getFunctions()Gets registered functions.
getGlobals()Gets the registered Globals.
getLoader()Gets the Loader instance.
getNodeVisitors()Gets the registered Node Visitors.
getRuntime()Returns the runtime implementation of a Twig element (filter/function/test).
getTags()Gets registered tags.
getTemplateClass()Gets the template class associated with the given string.
getTest()Gets a test by name.
getTests()Gets the registered Tests.
getTokenParsers()Gets the registered Token Parsers.
getUnaryOperators()Gets the registered unary Operators.
hasExtension()Returns true if the given extension is registered.
isAutoReload()Checks if the auto_reload option is enabled.
isDebug()Checks if debug mode is enabled.
isStrictVariables()Checks if the strict_variables option is enabled.
isTemplateFresh()Returns true if the template is still fresh.
load()Loads a template.
loadTemplate()Loads a template internal representation.
mergeGlobals()Merges a context with the defined globals.
parse()Converts a token stream to a node tree.
registerUndefinedFilterCallback()
registerUndefinedFunctionCallback()
render()Renders a template.
resolveTemplate()Tries to load a template consecutively from an array.
setBaseTemplateClass()Sets the base template class for compiled templates.
setCache()Sets the current cache implementation.
setCharset()Sets the default template charset.
setCompiler()
setDefaultEscaperStrategy()
setExtensions()Registers an array of extensions.
setLexer()
setLoader()
setParser()
tokenize()Tokenizes a source code.

__construct()

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).

View source

Arguments

Signature

public void __construct ( Twig_LoaderInterface $loader, array $options = [] )

compileSource()

Compiles a template source code.

View source

Arguments

  • $source

Returns

string – The compiled PHP source code

Throws

Signature

public string compileSource ( Twig_Source $source )

getDefaultEscaperStrategy()

Returns the default escaper strategy to use based on the template name.

View source

Arguments

Returns

string, false

Signature

public string, false getDefaultEscaperStrategy ( \craft\web\twig\string $name )

loadTemplate()

Loads a template internal representation.

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

View source

Arguments

  • $name (string) – The template name
  • $index (integer) – The index if it is an embedded template

Returns

Twig_Template – A template instance representing the given template name

Throws

Signature

public Twig_Template loadTemplate ( $name, $index = null )

setDefaultEscaperStrategy()

View source

Arguments

  • $strategy (mixed, null) – The escaper strategy to set. If null, it will be determined based on the template name.

Signature

public void setDefaultEscaperStrategy ( $strategy = null )

Constants

ConstantDescription
EXTRA_VERSION
MAJOR_VERSION
MINOR_VERSION
RELEASE_VERSION
VERSION
VERSION_ID