Skip to content

Template ​

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Template
Since
3.0.0

Class Template

View source

Public Methods ​

MethodDescription
beginProfile()Begins profiling a template element.
contextWithoutTemplate()Filters the template from a context array.
css()Registers a CSS file or a CSS code block.
endProfile()Finishes profiling a template element.
fallback()Provides dynamically-defined fallback variable’s value.
fallbackExists()Returns whether a fallback variable has been defined.
js()Registers a JS file or a JS code block.
paginateCriteria()Paginates a query.
paginateQuery()Paginates a query.
preloadSingles()Preloads Single section entries as fallback values for \craft\helpers\fallbackValue()
raw()Returns a string wrapped in a \Twig\Markup object
render()Renders the given renderable object(s).
resolveTemplatePathAndLine()Attempts to resolve a compiled template file path and line number to its source template path and line number.

beginProfile() ​

Since
3.3.0

Begins profiling a template element.

View source

Arguments ​

  • $type (string) – The type of template element being profiled ('template', 'block', or 'macro')
  • $name (string) – The name of the template element

contextWithoutTemplate() ​

Since
4.4.0

Filters the template from a context array.

Used by the dump() function and dd tags.

View source

Arguments ​

Returns ​

array

css() ​

Since
3.5.6

Registers a CSS file or a CSS code block.

View source

Arguments ​

  • $css (string) – The CSS file URL, or the content of the CSS code block to be registered
  • $options (array) – The HTML attributes for the <link>/<style> tag.
  • $key (string, null) – The key that identifies the CSS code block. If null, it will use $css as the key. If two CSS code blocks are registered with the same key, the latter will overwrite the former.

Throws ​

endProfile() ​

Since
3.3.0

Finishes profiling a template element.

View source

Arguments ​

  • $type (string) – The type of template element being profiled ('template', 'block', or 'macro')
  • $name (string) – The name of the template element

fallback() ​

DEPRECATED

Deprecated in 5.9.15

Since
4.4.0

Provides dynamically-defined fallback variable’s value.

View source

Arguments ​

Throws ​

fallbackExists() ​

DEPRECATED

Deprecated in 5.9.15

Since
4.4.0

Returns whether a fallback variable has been defined.

View source

Arguments ​

Returns ​

boolean

js() ​

Since
3.5.6

Registers a JS file or a JS code block.

View source

Arguments ​

  • $js (string) – The JS file URL, or the content of the JS code block to be registered
  • $options (array) – The HTML attributes for the <script> tag.
  • $key (string, null) – The key that identifies the JS code block. If null, it will use $css as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.

Throws ​

paginateCriteria() ​

DEPRECATED

Deprecated in 3.6.0. Use paginateQuery() instead.

Paginates a query.

View source

Arguments ​

Returns ​

array

paginateQuery() ​

Since
3.6.0

Paginates a query.

View source

Arguments ​

Returns ​

array

preloadSingles() ​

Since
4.4.0

Preloads Single section entries as fallback values for \craft\helpers\fallbackValue()

View source

Arguments ​

raw() ​

Returns a string wrapped in a \Twig\Markup object

View source

Arguments ​

Returns ​

\Twig\Markup

render() ​

Since
5.12.0

Renders the given renderable object(s).

If an array or other iterable is passed, each of its items will be rendered and joined with newlines, and a loop variable will be made available to each of them, like within {% for %} loops. Any loop variable in $context will be replaced, but a loop variable in $variables will be left alone.

View source

Arguments ​

  • $objects (craft\base\Renderable, iterable<\craft\base\Renderable>, null) – The object(s) to render
  • $variables (array) – Variables that should be passed to each object’s render() method
  • $context (array) – Additional variables that should be passed to each object’s render() method, such as the current template context. $variables will take precedence over them.

Returns ​

\Twig\Markup

Throws ​

resolveTemplatePathAndLine() ​

Since
4.1.5

Attempts to resolve a compiled template file path and line number to its source template path and line number.

View source

Arguments ​

  • $path (string) – The compiled template path
  • $line (integer, null) – The line number from the compiled template

Returns ​

array, false – The resolved template path and line number, or false if the path couldn’t be determined. If a template path could be determined but not the template line number, the line number will be null.

Constants ​

ConstantDescription
PROFILE_STAGE_BEGIN
PROFILE_STAGE_END
PROFILE_TYPE_BLOCK
PROFILE_TYPE_MACRO
PROFILE_TYPE_TEMPLATE