View

Type
Class
Namespace
craft\web
Inherits
craft\web\View » yii\web\View (opens new window) » yii\base\View (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window), yii\base\DynamicContentAwareInterface (opens new window)
Since
3.0.0

View source (opens new window)

# Public Properties

Property Description
allowEval boolean (opens new window) – Whether to allow evaluateDynamicContent() to be called.
assetBundles (opens new window) yii\web\AssetBundle (opens new window) – List of the registered asset bundles.
assetManager (opens new window) yii\web\AssetManager (opens new window) – The asset manager.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component.
blocks (opens new window) array (opens new window) – A list of named output blocks.
bodyHtml string (opens new window) – the content to be inserted at the end of the body section
context (opens new window) yii\base\ViewContextInterface (opens new window) – The context under which the renderFile() (opens new window) method is being invoked.
cpTemplateRoots array (opens new window) – any registered control panel template roots
css (opens new window) array (opens new window) – The registered CSS code blocks.
cssFiles (opens new window) array (opens new window) – The registered CSS files.
defaultExtension (opens new window) string (opens new window) – The default view file extension.
deltaNames string (opens new window)[]
dynamicContents (opens new window) yii\base\DynamicContentAwareInterface (opens new window) – Class instances supporting dynamic contents.
dynamicPlaceholders (opens new window) array (opens new window) – A list of placeholders.
headHtml string (opens new window) – the content to be inserted in the head section
initialDeltaValues array (opens new window)
isDeltaRegistrationActive boolean (opens new window)
isRenderingPageTemplate boolean (opens new window) – whether a page template is currently being rendered
isRenderingTemplate boolean (opens new window) – whether a template is currently being rendered
js (opens new window) array (opens new window) – The registered JS code blocks
jsFiles (opens new window) array (opens new window) – The registered JS files.
linkTags (opens new window) array (opens new window) – The registered link tags.
metaTags (opens new window) array (opens new window) – The registered meta tags.
namespace string (opens new window), null (opens new window) – the active namespace
params (opens new window) array (opens new window) – Custom parameters that are shared among view templates.
registeredAssetBundles string (opens new window)[] – the asset bundle names that should be marked as already registered
registeredJsFiles string (opens new window)[] – the JS files that should be marked as already registered
renderers (opens new window) array (opens new window) – A list of available renderers indexed by their corresponding supported file extensions.
siteTemplateRoots array (opens new window) – any registered site template roots
templateMode string (opens new window) – the current template mode (either site or cp)
templatesPath string (opens new window) – the base path that templates should be found in
theme (opens new window) yii\base\Theme (opens new window), array (opens new window), string (opens new window) – The theme object or the configuration for creating the theme object.
title (opens new window) string (opens new window) – The page title
twig craft\web\twig\Environment – the Twig environment
viewFile (opens new window) string (opens new window), boolean (opens new window) – The view file currently being rendered.

# allowEval

Type
boolean (opens new window)
Default value
false
Since
3.5.0

Whether to allow evaluateDynamicContent() to be called.

WARNING

Don’t enable this unless you have a very good reason to.

View source (opens new window)

# bodyHtml

Type
string (opens new window)
Default value
null

the content to be inserted at the end of the body section

View source (opens new window)

# cpTemplateRoots

Type
array (opens new window)
Default value
null

any registered control panel template roots

View source (opens new window)

# deltaNames

Type
string (opens new window)[]
Default value
null
Access
Read-only
Since
3.4.0

View source (opens new window)

# headHtml

Type
string (opens new window)
Default value
null

the content to be inserted in the head section

View source (opens new window)

# initialDeltaValues

Type
array (opens new window)
Default value
null
Access
Read-only
Since
3.7.0

View source (opens new window)

# isDeltaRegistrationActive

Type
boolean (opens new window)
Default value
null
Since
3.4.0

View source (opens new window)

# isRenderingPageTemplate

Type
boolean (opens new window)
Default value
null

whether a page template is currently being rendered

View source (opens new window)

# isRenderingTemplate

Type
boolean (opens new window)
Default value
null

whether a template is currently being rendered

View source (opens new window)

# namespace

Type
string (opens new window), null (opens new window)
Default value
null

the active namespace

View source (opens new window)

# registeredAssetBundles

Type
string (opens new window)[]
Default value
null

the asset bundle names that should be marked as already registered

View source (opens new window)

# registeredJsFiles

Type
string (opens new window)[]
Default value
null

the JS files that should be marked as already registered

View source (opens new window)

# siteTemplateRoots

Type
array (opens new window)
Default value
null

any registered site template roots

View source (opens new window)

# templateMode

Type
string (opens new window)
Default value
null

the current template mode (either site or cp)

View source (opens new window)

# templatesPath

Type
string (opens new window)
Default value
null

the base path that templates should be found in

View source (opens new window)

# twig

Type
craft\web\twig\Environment
Default value
null

the Twig environment

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of a component property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets the value of a component property.
__unset() (opens new window) Sets a component property to be null.
addDynamicPlaceholder() (opens new window) Adds a placeholder for dynamic content.
afterRender() (opens new window) This method is invoked right after renderFile() (opens new window) renders a view file.
afterRenderPageTemplate() Performs actions after a page template is rendered.
afterRenderTemplate() Performs actions after a template is rendered.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
beforeRender() (opens new window) This method is invoked right before renderFile() (opens new window) renders a view file.
beforeRenderPageTemplate() Performs actions before a page template is rendered.
beforeRenderTemplate() Performs actions before a template is rendered.
beginBlock() (opens new window) Begins recording a block.
beginBody() (opens new window) Marks the beginning of an HTML body section.
beginCache() (opens new window) Begins fragment caching.
beginContent() (opens new window) Begins the rendering of content that is to be decorated by the specified view.
beginPage() (opens new window) Marks the beginning of a page.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
clear() (opens new window) Clears up the registered meta tags, link tags, css/js scripts and files.
clearCssBuffer() Clears and ends a buffer started via startCssBuffer(), returning any <style> tags that were registered while the buffer was active.
clearCssFileBuffer() Clears and ends a buffer started via startCssFileBuffer(), returning any <link rel="stylesheet"> tags that were registered while the buffer was active.
clearJsBuffer() Clears and ends a buffer started via startJsBuffer(), returning any JavaScript code that was registered while the buffer was active.
clearJsFileBuffer() Clears and ends a buffer started via startJsFileBuffer(), returning any <script> tags that were registered while the buffer was active.
clearScriptBuffer() Clears and ends a buffer started via startScriptBuffer(), returning any <script> tags that were registered while the buffer was active.
createTwig() Creates a new Twig environment.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
doesTemplateExist() Returns whether a template exists.
endBlock() (opens new window) Ends recording a block.
endBody() Marks the ending of an HTML body section.
endCache() (opens new window) Ends fragment caching.
endContent() (opens new window) Ends the rendering of content.
endPage() Marks the ending of an HTML page.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
evaluateDynamicContent() Evaluates the given PHP statements.
formatInputId() Formats an ID out of an input name.
getAssetManager() (opens new window) Registers the asset manager being used by this view object.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getBodyHtml() Returns the content to be inserted at the end of the body section.
getCpTemplateRoots() Returns any registered control panel template roots.
getDeltaNames() Returns all of the registered delta input names.
getDynamicContents() (opens new window) Returns a list of currently active dynamic content class instances.
getDynamicPlaceholders() (opens new window) Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.
getHeadHtml() Returns the content to be inserted in the head section.
getInitialDeltaValues() Returns the initial values of delta inputs.
getIsDeltaRegistrationActive() Returns whether delta input name registration is currently active
getIsRenderingPageTemplate() Returns whether a page template is currently being rendered.
getIsRenderingTemplate() Returns whether a template is currently being rendered.
getNamespace() Returns the active namespace.
getSiteTemplateRoots() Returns any registered site template roots.
getTemplateMode() Returns the current template mode (either site or cp).
getTemplatesPath() Returns the base path that templates should be found in.
getTwig() Returns the Twig environment.
getViewFile() (opens new window)
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined for this component.
head() (opens new window) Marks the position of an HTML head section.
hook() Queues up a method to be called by a given template hook.
init() Initializes the view component.
invokeHook() Invokes a template hook.
namespaceInputId() Namespaces an input ID.
namespaceInputName() Namespaces an input name.
namespaceInputs() Renames HTML input names so they belong to a namespace.
normalizeObjectTemplate() Normalizes an object template for renderObjectTemplate().
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
popDynamicContent() (opens new window) Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances.
pushDynamicContent() (opens new window) Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances.
registerAssetBundle() (opens new window) Registers the named asset bundle.
registerCsrfMetaTags() (opens new window) Registers CSRF meta tags.
registerCss() (opens new window) Registers a CSS code block.
registerCssFile() (opens new window) Registers a CSS file.
registerDeltaName() Registers a delta input name.
registerHtml() Registers arbitrary HTML to be injected into the final page response.
registerJs() Registers a JS code block.
registerJsFile() Registers a JS file.
registerJsVar() (opens new window) Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.
registerJsWithVars() Registers JavaScript code with the given variables, pre-JSON-encoded.
registerLinkTag() (opens new window) Registers a link tag.
registerMetaTag() (opens new window) Registers a meta tag.
registerScript() Registers a generic <script> code block.
registerTranslations() Translates messages for a given translation category, so they will be available for Craft.t() calls in the control panel.
registerTwigExtension() Registers a new Twig extension, which will be added on existing environments and queued up for future environments.
render() (opens new window) Renders a view.
renderAjax() (opens new window) Renders a view in response to an AJAX request.
renderDynamic() (opens new window) Renders dynamic content returned by the given PHP statements.
renderFile() (opens new window) Renders a view file.
renderObjectTemplate() Renders an object template.
renderPageTemplate() Renders a Twig template that represents an entire web page.
renderPhpFile() (opens new window) Renders a view file as a PHP script.
renderString() Renders a template defined in a string.
renderTemplate() Renders a Twig template.
resolveTemplate() Finds a template on the file system and returns its path.
setAssetManager() (opens new window) Sets the asset manager.
setDynamicPlaceholders() (opens new window) Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.
setInitialDeltaValue() Sets the initial value of a delta input name.
setIsDeltaRegistrationActive() Sets whether delta input name registration is active.
setNamespace() Sets the active namespace.
setRegisteredAssetBundles() Sets the asset bundle names that should be marked as already registered.
setRegisteredJsFiles() Sets the JS files that should be marked as already registered.
setTemplateMode() Sets the current template mode.
setTemplatesPath() Sets the base path that templates should be found in.
startCssBuffer() Starts a buffer for any <style> tags registered with registerCss() (opens new window).
startCssFileBuffer() Starts a buffer for any <link> tags registered with registerCssFile() (opens new window).
startJsBuffer() Starts a buffer for any JavaScript code registered with registerJs().
startJsFileBuffer() Starts a buffer for any <script> tags registered with registerJsFile().
startScriptBuffer() Starts a buffer for any <script> tags registered with registerScript().
trigger() (opens new window) Triggers an event.

# afterRenderPageTemplate()

Performs actions after a page template is rendered.

View source (opens new window)

Arguments

# afterRenderTemplate()

Performs actions after a template is rendered.

View source (opens new window)

Arguments

# beforeRenderPageTemplate()

Performs actions before a page template is rendered.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the template should be rendered

# beforeRenderTemplate()

Performs actions before a template is rendered.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the template should be rendered

# clearCssBuffer()

Since
3.7.0

Clears and ends a buffer started via startCssBuffer(), returning any <style> tags that were registered while the buffer was active.

See also startCssBuffer() View source (opens new window)

Returns

array (opens new window), false (opens new window) – The <style> tags that were registered while the buffer was active, or false if there wasn’t an active buffer.

# clearCssFileBuffer()

Since
4.0.0

Clears and ends a buffer started via startCssFileBuffer(), returning any <link rel="stylesheet"> tags that were registered while the buffer was active.

See also startCssFileBuffer() View source (opens new window)

Returns

array (opens new window), false (opens new window) – The <link rel="stylesheet"> tags that were registered while the buffer was active, or false if there wasn’t an active buffer.

# clearJsBuffer()

Clears and ends a buffer started via startJsBuffer(), returning any JavaScript code that was registered while the buffer was active.

See also startJsBuffer() View source (opens new window)

Arguments

  • $scriptTag (boolean (opens new window)) – Whether the returned JavaScript code should be wrapped in a <script> tag.
  • $combine (boolean (opens new window)) – Whether the JavaScript code should be returned in a combined blob. (Position and key info will be lost.)

Returns

string (opens new window), array (opens new window), false (opens new window) – The JavaScript code that was registered while the buffer was active, or false if there wasn’t an active buffer.

# clearJsFileBuffer()

Since
4.0.0

Clears and ends a buffer started via startJsFileBuffer(), returning any <script> tags that were registered while the buffer was active.

See also startJsFileBuffer() View source (opens new window)

Returns

array (opens new window), false (opens new window) – The <script> tags that were registered while the buffer was active (indexed by position), or false if there wasn’t an active buffer.

# clearScriptBuffer()

Since
3.7.0

Clears and ends a buffer started via startScriptBuffer(), returning any <script> tags that were registered while the buffer was active.

See also startScriptBuffer() View source (opens new window)

Returns

array (opens new window), false (opens new window) – The <script> tags that were registered while the buffer was active, or false if there wasn’t an active buffer.

# createTwig()

Creates a new Twig environment.

View source (opens new window)

Returns

craft\web\twig\Environment

# doesTemplateExist()

Returns whether a template exists.

Internally, this will just call resolveTemplate() with the given template name, and return whether that method found anything.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the template exists.

Throws

# endBody()

Marks the ending of an HTML body section.

View source (opens new window)

# endPage()

Marks the ending of an HTML page.

View source (opens new window)

Arguments

# evaluateDynamicContent()

Evaluates the given PHP statements.

This method is mainly used internally to implement dynamic content feature.

View source (opens new window)

Arguments

Returns

mixed – The return value of the PHP statements.

Throws

# formatInputId()

DEPRECATED

Deprecated in 3.5.0. Use craft\helpers\Html::id() instead.

Formats an ID out of an input name.

This method takes a given input name and returns a valid ID based on it. For example, if given the following input name: foo[bar][title] the following ID would be returned: foo-bar-title

View source (opens new window)

Arguments

Returns

string (opens new window) – The input ID.

# getBodyHtml()

Returns the content to be inserted at the end of the body section.

This includes:

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendered content

# getCpTemplateRoots()

Returns any registered control panel template roots.

View source (opens new window)

Returns

array (opens new window)

# getDeltaNames()

Since
3.4.0

Returns all of the registered delta input names.

See also registerDeltaName() View source (opens new window)

Returns

string (opens new window)[]

# getHeadHtml()

Returns the content to be inserted in the head section.

This includes:

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendered content

# getInitialDeltaValues()

Since
3.7.0

Returns the initial values of delta inputs.

See also setInitialDeltaValue() View source (opens new window)

Returns

array (opens new window)

# getIsDeltaRegistrationActive()

Since
3.4.0

Returns whether delta input name registration is currently active

See also registerDeltaName() View source (opens new window)

Returns

boolean (opens new window)

# getIsRenderingPageTemplate()

Returns whether a page template is currently being rendered.

View source (opens new window)

Returns

boolean (opens new window) – Whether a page template is currently being rendered.

# getIsRenderingTemplate()

Returns whether a template is currently being rendered.

View source (opens new window)

Returns

boolean (opens new window) – Whether a template is currently being rendered.

# getNamespace()

Returns the active namespace.

This is the default namespaces that will be used when namespaceInputs(), namespaceInputName(), and namespaceInputId() are called, if their $namespace arguments are null.

View source (opens new window)

Returns

string (opens new window), null (opens new window) – The namespace.

# getSiteTemplateRoots()

Returns any registered site template roots.

View source (opens new window)

Returns

array (opens new window)

# getTemplateMode()

Returns the current template mode (either site or cp).

View source (opens new window)

Returns

string (opens new window) – Either site or cp.

# getTemplatesPath()

Returns the base path that templates should be found in.

View source (opens new window)

Returns

string (opens new window)

# getTwig()

Returns the Twig environment.

View source (opens new window)

Returns

craft\web\twig\Environment

# hook()

Queues up a method to be called by a given template hook.

For example, if you place this in your plugin’s init() (opens new window) method:

Craft::$app->view->hook('myAwesomeHook', function(&$context) {
    $context['foo'] = 'bar';
    return 'Hey!';
});

you would then be able to add this to any template:

{% hook "myAwesomeHook" %}

When the hook tag gets invoked, your template hook function will get called. The $context argument will be the current Twig context array, which you’re free to manipulate. Any changes you make to it will be available to the template following the tag. Whatever your template hook function returns will be output in place of the tag in the template as well.

If you want to prevent additional hook methods from getting triggered, add a second $handled argument to your callback method, which should be passed by reference, and then set it to true within the method.

Craft::$app->view->hook('myAwesomeHook', function(&$context, &$handled) {
    $context['foo'] = 'bar';
    $handled = true;
    return 'Hey!';
});

View source (opens new window)

Arguments

# init()

Initializes the view component.

View source (opens new window)

# invokeHook()

Invokes a template hook.

This is called by \craft\web\HookNode.

View source (opens new window)

Arguments

Returns

string (opens new window) – Whatever the hooks returned.

# namespaceInputId()

Namespaces an input ID.

This method applies the same namespacing treatment that namespaceInputs() does to id= attributes, but only to a single value, which is passed directly into this method.

View source (opens new window)

Arguments

Returns

string (opens new window) – The namespaced input ID.

# namespaceInputName()

Namespaces an input name.

This method applies the same namespacing treatment that namespaceInputs() does to name= attributes, but only to a single value, which is passed directly into this method.

View source (opens new window)

Arguments

Returns

string (opens new window) – The namespaced input name.

# namespaceInputs()

Renames HTML input names so they belong to a namespace.

This method will go through the passed-in $html looking for name= attributes, and renaming their values such that they will live within the passed-in $namespace (or the active namespace). By default, any id=, for=, list=, data-target=, data-reverse-target=, and data-target-prefix= attributes will get namespaced as well, by prepending the namespace and a dash to their values. For example, the following HTML:

<label for="title">Title</label>
<input type="text" name="title" id="title">

would become this, if it were namespaced with “foo”:

<label for="foo-title">Title</label>
<input type="text" name="foo[title]" id="foo-title">

Attributes that are already namespaced will get double-namespaced. For example, the following HTML:

<label for="bar-title">Title</label>
<input type="text" name="bar[title]" id="bar-title">

would become:

<label for="foo-bar-title">Title</label>
<input type="text" name="foo[bar][title]" id="foo-bar-title">

When a callable is passed to $html (supported as of Craft 3.7), the namespace will be set via setNamespace() before the callable is executed, in time for any JavaScript code that needs to be registered by the callable.

$settingsHtml = Craft::$app->view->namespaceInputs(function() use ($widget) {
    return $widget->getSettingsHtml();
}, 'widget-settings');

View source (opens new window)

Arguments

Returns

string (opens new window) – The HTML with namespaced attributes

# normalizeObjectTemplate()

Normalizes an object template for renderObjectTemplate().

View source (opens new window)

Arguments

Returns

string (opens new window)

# registerDeltaName()

Since
3.4.0

Registers a delta input name.

This can be either the name of a single form input, or a prefix used by multiple input names.

The input name will be namespaced with the currently active namespace, if any.

When a form that supports delta updates is submitted, any delta inputs (or groups of inputs) that didn’t change over the lifespan of the page will be omitted from the POST request.

Note that delta input names will only be registered if delta registration is active (see getIsDeltaRegistrationActive()).

View source (opens new window)

Arguments

# registerHtml()

Since
3.5.0

Registers arbitrary HTML to be injected into the final page response.

View source (opens new window)

Arguments

# registerJs()

Registers a JS code block.

View source (opens new window)

Arguments

# registerJsFile()

Registers a JS file.

This method should be used for simple registration of JS files. If you want to use features of craft\web\AssetManager like appending timestamps to the URL and file publishing options, use craft\web\AssetBundle and registerAssetBundle() (opens new window) instead.

View source (opens new window)

Arguments

  • $url (string (opens new window)) – The JS file to be registered.

  • $options (array (opens new window)) – The HTML attributes for the script tag. The following options are specially handled and are not treated as HTML attributes:

  • depends: array, specifies the names of the asset bundles that this JS file depends on.

  • position: specifies where the JS script tag should be inserted in a page. The possible values are:

  • appendTimestamp: bool whether to append a timestamp to the URL.

Please refer to craft\helpers\Html::jsFile() (opens new window) for other supported options.

  • $key (string (opens new window)) – The key that identifies the JS script file. If null, it will use $url as the key. If two JS files are registered with the same key at the same position, the latter will overwrite the former. Note that position option takes precedence, thus files registered with the same key, but different position option will not override each other.

Throws

# registerJsWithVars()

Since
3.7.31

Registers JavaScript code with the given variables, pre-JSON-encoded.

View source (opens new window)

Arguments

# registerScript()

Registers a generic <script> code block.

View source (opens new window)

Arguments

# registerTranslations()

Translates messages for a given translation category, so they will be available for Craft.t() calls in the control panel.

Note this should always be called before any JavaScript is registered that will need to use the translations, unless the JavaScript is registered at yii\web\View::POS_READY (opens new window).

View source (opens new window)

Arguments

# registerTwigExtension()

Registers a new Twig extension, which will be added on existing environments and queued up for future environments.

View source (opens new window)

Arguments

  • $extension (\Twig\Extension\ExtensionInterface)

# renderObjectTemplate()

Renders an object template.

The passed-in $object will be available to the template as an object variable.

The template will be parsed for “property tags” (e.g. {foo}), which will get replaced with full Twig output tags (e.g. {{ object.foo|raw }}.

If $object is an instance of yii\base\Arrayable (opens new window), any attributes returned by its fields() (opens new window) or extraFields() (opens new window) methods will also be available as variables to the template.

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendered template.

Throws

# renderPageTemplate()

Renders a Twig template that represents an entire web page.

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendering result

Throws

# renderString()

Renders a template defined in a string.

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendered template.

Throws

  • \Twig\Error\LoaderError
  • \Twig\Error\SyntaxError

# renderTemplate()

Renders a Twig template.

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendering result

Throws

# resolveTemplate()

Finds a template on the file system and returns its path.

All of the following files will be searched for, in this order:

  • TemplateName
  • TemplateName.html
  • TemplateName.twig
  • TemplateName/index.html
  • TemplateName/index.twig

If this is a front-end request, the actual list of file extensions and index filenames are configurable via the config4:defaultTemplateExtensions (opens new window) and config4:indexTemplateFilenames (opens new window) config settings.

For example if you set the following in config/general.php:

'defaultTemplateExtensions' => ['htm'],
'indexTemplateFilenames' => ['default'],

then the following files would be searched for instead:

  • TemplateName
  • TemplateName.htm
  • TemplateName/default.htm

The actual directory that those files will depend on the current template mode (probably templates/ if it’s a front-end site request, and vendor/craftcms/cms/src/templates/ if it’s a Control Panel request).

If this is a front-end site request, a folder named after the current site handle will be checked first.

  • templates/SiteHandle/...
  • templates/...

And finally, if this is a control panel request and the template name includes multiple segments and the first segment of the template name matches a plugin’s handle, then Craft will look for a template named with the remaining segments within that plugin’s templates/ subfolder.

To put it all together, here’s where Craft would look for a template named “foo/bar”, depending on the type of request it is:

  • Front-end site requests:
    • templates/SiteHandle/foo/bar
    • templates/SiteHandle/foo/bar.html
    • templates/SiteHandle/foo/bar.twig
    • templates/SiteHandle/foo/bar/index.html
    • templates/SiteHandle/foo/bar/index.twig
    • templates/foo/bar
    • templates/foo/bar.html
    • templates/foo/bar.twig
    • templates/foo/bar/index.html
    • templates/foo/bar/index.twig
  • Control panel requests:
    • vendor/craftcms/cms/src/templates/foo/bar
    • vendor/craftcms/cms/src/templates/foo/bar.html
    • vendor/craftcms/cms/src/templates/foo/bar.twig
    • vendor/craftcms/cms/src/templates/foo/bar/index.html
    • vendor/craftcms/cms/src/templates/foo/bar/index.twig
    • path/to/fooplugin/templates/bar
    • path/to/fooplugin/templates/bar.html
    • path/to/fooplugin/templates/bar.twig
    • path/to/fooplugin/templates/bar/index.html
    • path/to/fooplugin/templates/bar/index.twig

View source (opens new window)

Arguments

Returns

string (opens new window), false (opens new window) – The path to the template if it exists, or false.

Throws

  • \Twig\Error\LoaderError

# setInitialDeltaValue()

Since
3.4.6

Sets the initial value of a delta input name.

See also getInitialDeltaValues() View source (opens new window)

Arguments

# setIsDeltaRegistrationActive()

Since
3.4.0

Sets whether delta input name registration is active.

See also registerDeltaName() View source (opens new window)

Arguments

# setNamespace()

Sets the active namespace.

This is the default namespaces that will be used when namespaceInputs(), namespaceInputName(), and namespaceInputId() are called, if their|null $namespace arguments are null.

View source (opens new window)

Arguments

# setRegisteredAssetBundles()

Since
3.0.10

Sets the asset bundle names that should be marked as already registered.

View source (opens new window)

Arguments

# setRegisteredJsFiles()

Since
3.0.10

Sets the JS files that should be marked as already registered.

View source (opens new window)

Arguments

# setTemplateMode()

Sets the current template mode.

The template mode defines:

  • the base path that templates should be looked for in
  • the default template file extensions that should be automatically added when looking for templates
  • the "index" template filenames that should be checked when looking for templates

View source (opens new window)

Arguments

Throws

# setTemplatesPath()

Sets the base path that templates should be found in.

View source (opens new window)

Arguments

# startCssBuffer()

Since
3.7.0

Starts a buffer for any <style> tags registered with registerCss() (opens new window).

The buffer’s contents can be cleared and returned later via clearCssBuffer().

See also clearCssBuffer() View source (opens new window)

# startCssFileBuffer()

Since
4.0.0

Starts a buffer for any <link> tags registered with registerCssFile() (opens new window).

The buffer’s contents can be cleared and returned later via clearCssFileBuffer().

See also clearCssFileBuffer() View source (opens new window)

# startJsBuffer()

Starts a buffer for any JavaScript code registered with registerJs().

The buffer’s contents can be cleared and returned later via clearJsBuffer().

See also clearJsBuffer() View source (opens new window)

# startJsFileBuffer()

Since
4.0.0

Starts a buffer for any <script> tags registered with registerJsFile().

The buffer’s contents can be cleared and returned later via clearJsFileBuffer().

See also clearJsFileBuffer() View source (opens new window)

# startScriptBuffer()

Since
3.7.0

Starts a buffer for any <script> tags registered with registerScript().

The buffer’s contents can be cleared and returned later via clearScriptBuffer().

See also clearScriptBuffer() View source (opens new window)

# Protected Methods

Method Description
findViewFile() (opens new window) Finds the view file based on the given view name.
getRequestedViewFile() (opens new window)
registerAllAssetFiles() Registers all files provided by all registered asset bundles, including depending bundles files.
registerAssetFiles() Registers all files provided by an asset bundle including depending bundles files.
registerAssetFlashes() Registers any asset bundles and JS code that were queued-up in the session flash data.
renderBodyBeginHtml() Renders the content to be inserted at the beginning of the body section.
renderBodyEndHtml() Renders the content to be inserted at the end of the body section.
renderHeadHtml() Renders the content to be inserted in the head section.

# registerAllAssetFiles()

Registers all files provided by all registered asset bundles, including depending bundles files.

Removes a bundle from assetBundles (opens new window) once files are registered.

View source (opens new window)

# registerAssetFiles()

Registers all files provided by an asset bundle including depending bundles files.

Removes a bundle from assetBundles (opens new window) once files are registered.

View source (opens new window)

Arguments

# registerAssetFlashes()

Registers any asset bundles and JS code that were queued-up in the session flash data.

View source (opens new window)

Throws

# renderBodyBeginHtml()

Renders the content to be inserted at the beginning of the body section.

The content is rendered using the registered JS code blocks and files.

View source (opens new window)

Returns

string (opens new window) – The rendered content

# renderBodyEndHtml()

Renders the content to be inserted at the end of the body section.

The content is rendered using the registered JS code blocks and files.

View source (opens new window)

Arguments

Returns

string (opens new window) – The rendered content

# renderHeadHtml()

Renders the content to be inserted in the head section.

The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.

View source (opens new window)

Returns

string (opens new window) – The rendered content

# Constants

Constant Description
PH_BODY_BEGIN This is internally used as the placeholder for receiving the content registered for the beginning of the body section.
PH_BODY_END This is internally used as the placeholder for receiving the content registered for the end of the body section.
PH_HEAD This is internally used as the placeholder for receiving the content registered for the head section.
POS_BEGIN The location of registered JavaScript code block or files.
POS_END The location of registered JavaScript code block or files.
POS_HEAD The location of registered JavaScript code block or files.
POS_LOAD The location of registered JavaScript code block.
POS_READY The location of registered JavaScript code block.
TEMPLATE_MODE_CP
TEMPLATE_MODE_SITE

# Events

# EVENT_AFTER_RENDER_PAGE_TEMPLATE

Type
craft\events\TemplateEvent

The event that is triggered after a page template gets rendered

# EVENT_AFTER_RENDER_TEMPLATE

Type
craft\events\TemplateEvent

The event that is triggered after a template gets rendered

# EVENT_BEFORE_RENDER_PAGE_TEMPLATE

Type
craft\events\TemplateEvent

The event that is triggered before a page template gets rendered

# EVENT_BEFORE_RENDER_TEMPLATE

Type
craft\events\TemplateEvent

The event that is triggered before a template gets rendered

# EVENT_REGISTER_CP_TEMPLATE_ROOTS

Type
craft\events\RegisterTemplateRootsEvent

The event that is triggered when registering control panel template roots

# EVENT_REGISTER_SITE_TEMPLATE_ROOTS

Type
craft\events\RegisterTemplateRootsEvent

The event that is triggered when registering site template roots