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
# 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)
# minifyCss
DEPRECATED
Deprecated in 3.6.0.
- Type
- boolean (opens new window)
- Default value
false
- Since
- 3.4.0
Whether to minify CSS registered with registerCss() (opens new window)
View source (opens new window)
# minifyJs
DEPRECATED
Deprecated in 3.6.0
- Type
- boolean (opens new window)
- Default value
false
- Since
- 3.4.0
Whether to minify JS registered with registerJs()
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)
# Protected Properties
Property | Description |
---|---|
isPageEnded (opens new window) | boolean (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. |
clearHtmlBuffer() | Clears and ends a buffer started via startHtmlBuffer(), returning any html 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() | 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). |
startHtmlBuffer() | Starts a buffer for any html tags registered with registerHtml(). |
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
$template
(string (opens new window)) – The name of the template that was rendered$variables
(array (opens new window)) – The variables that were available to the template$templateMode
(string (opens new window)) – The template mode that was used when rendering the template$output
(string (opens new window)) – The template’s rendering result
# afterRenderTemplate()
Performs actions after a template is rendered.
View source (opens new window)
Arguments
$template
(string (opens new window)) – The name of the template that was rendered$variables
(array (opens new window)) – The variables that were available to the template$templateMode
(string (opens new window)) – The template mode that was used when rendering the template$output
(string (opens new window)) – The template’s rendering result
# beforeRenderPageTemplate()
Performs actions before a page template is rendered.
View source (opens new window)
Arguments
$template
(string (opens new window)) – The name of the template to render$variables
(array (opens new window)) – The variables that should be available to the template$templateMode
(string (opens new window)) – The template mode to use when rendering the template
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
$template
(string (opens new window)) – The name of the template to render$variables
(array (opens new window)) – The variables that should be available to the template$templateMode
(string (opens new window)) – The template mode to use when rendering the template
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.
# clearHtmlBuffer()
- Since
- 4.3.0
Clears and ends a buffer started via startHtmlBuffer(), returning any html tags that were registered while the buffer was active.
View source (opens new window)
Returns
array (opens new window), false (opens new window) – The html that was 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
# 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
$name
(string (opens new window)) – The name of the template.$templateMode
(string (opens new window), null (opens new window)) – The template mode to use.$publicOnly
(boolean (opens new window)) – Whether to only look for public templates (template paths that don’t start with the private template trigger).
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
$ajaxMode
(boolean (opens new window)) – Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY (opens new window) and POS_LOAD (opens new window) positions will be rendered at the end of the view like normal scripts.
# evaluateDynamicContent()
Evaluates the given PHP statements.
This method is mainly used internally to implement dynamic content feature.
View source (opens new window)
Arguments
$statements
(string (opens new window)) – The PHP statements to be evaluated.
Returns
mixed
– The return value of the PHP statements.
Throws
- yii\base\NotSupportedException (opens new window)
unless allowEval has been set totrue
.
# 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
$inputName
(string (opens new window)) – The input name.
Returns
string (opens new window) – The input ID.
# getBodyHtml()
Returns the content to be inserted at the end of the body section.
This includes:
- JS code registered with registerJs() with the position set to POS_BEGIN (opens new window), POS_END (opens new window), POS_READY (opens new window), or POS_LOAD (opens new window)
- JS files registered with registerJsFile() with the position set to POS_BEGIN (opens new window) or POS_END (opens new window)
View source (opens new window)
Arguments
$clear
(boolean (opens new window)) – Whether the content should be cleared from the queue (default is true)
Returns
string (opens new window) – The rendered content
# getCpTemplateRoots()
Returns any registered control panel template roots.
View source (opens new window)
Returns
# getDeltaNames()
- Since
- 3.4.0
Returns all of the registered delta input names.
See also registerDeltaName() View source (opens new window)
Returns
# getHeadHtml()
Returns the content to be inserted in the head section.
This includes:
- Meta tags registered using registerMetaTag() (opens new window)
- Link tags registered with registerLinkTag() (opens new window)
- CSS code registered with registerCss() (opens new window)
- CSS files registered with registerCssFile() (opens new window)
- JS code registered with registerJs() with the position set to POS_HEAD (opens new window)
- JS files registered with registerJsFile() with the position set to POS_HEAD (opens new window)
View source (opens new window)
Arguments
$clear
(boolean (opens new window)) – Whether the content should be cleared from the queue (default is true)
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
# getIsDeltaRegistrationActive()
- Since
- 3.4.0
Returns whether delta input name registration is currently active
See also registerDeltaName() View source (opens new window)
Returns
# 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
# 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
# getTwig()
Returns the Twig environment.
View source (opens new window)
Returns
# 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
$hook
(string (opens new window)) – The hook name.$method
(callable (opens new window)) – The callback function.$append
(boolean (opens new window)) – Whether to append the method handler to the end of the existing method list for the hook. Iffalse
, the method will be inserted at the beginning of the existing method list.
# 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
$hook
(string (opens new window)) – The hook name.$context
(array (opens new window)) – The current template context.
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
$inputId
(string (opens new window)) – The input ID that should be namespaced.$namespace
(string (opens new window), null (opens new window)) – The namespace. Defaults to the active namespace.
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
$inputName
(string (opens new window)) – The input name that should be namespaced.$namespace
(string (opens new window), null (opens new window)) – The namespace. Defaults to the active namespace.
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
$html
(callable (opens new window), string (opens new window)) – The HTML code, or a callable that returns the HTML code$namespace
(string (opens new window), null (opens new window)) – The namespace. Defaults to the active namespace.$otherAttributes
(boolean (opens new window)) – Whetherid
,for
, and other attributes should be namespaced (in addition toname
)$withClasses
(boolean (opens new window)) – Whether class names should be namespaced as well (affects bothclass
attributes and class name CSS selectors within<style>
tags). This will only have an effect if$otherAttributes
istrue
.
Returns
string (opens new window) – The HTML with namespaced attributes
# normalizeObjectTemplate()
Normalizes an object template for renderObjectTemplate().
View source (opens new window)
Arguments
$template
(string (opens new window))
Returns
# registerAssetBundle()
Registers the named asset bundle.
All dependent asset bundles will be registered.
View source (opens new window)
Arguments
$name
(string (opens new window)) – The class name of the asset bundle (without the leading backslash)$position
(integer (opens new window), null (opens new window)) – If set, this forces a minimum position for javascript files. This will adjust depending assets javascript file position or fail if requirement can not be met. If this is null, asset bundles position settings will not be changed. See registerJsFile() for more details on javascript position.
Returns
yii\web\AssetBundle (opens new window) – The registered asset bundle instance
Throws
- yii\base\InvalidConfigException (opens new window)
if the asset bundle does not exist or a circular dependency is detected
# 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
$inputName
(string (opens new window))
# registerHtml()
- Since
- 3.5.0
Registers arbitrary HTML to be injected into the final page response.
View source (opens new window)
Arguments
$html
(string (opens new window)) – The HTML code to be registered$position
(integer (opens new window)) – The position at which the HTML code should be inserted in the page. Possible values are:- POS_HEAD (opens new window): in the head section
- POS_BEGIN (opens new window): at the beginning of the body section
- POS_END (opens new window): at the end of the body section
$key
(string (opens new window), null (opens new window)) – The key that identifies the HTML code. If null, it will use a hash of the HTML as the key. If two HTML code blocks are registered with the same position and key, the latter will overwrite the former.
# registerJs()
Registers a JS code block.
View source (opens new window)
Arguments
$js
(string (opens new window)) – The JS code block to be registered$position
(integer (opens new window)) – The position at which the JS script tag should be inserted in a page. The possible values are:POS_HEAD (opens new window): in the head section
POS_BEGIN (opens new window): at the beginning of the body section
POS_END (opens new window): at the end of the body section
POS_LOAD (opens new window): enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
POS_READY (opens new window): enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
$key
(string (opens new window), null (opens new window)) – The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.
# 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() 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:- POS_HEAD (opens new window): in the head section
- POS_BEGIN (opens new window): at the beginning of the body section
- POS_END (opens new window): at the end of the body section. This is the default value.
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), null (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
$jsFn
(callable (opens new window)) – Callback function that returns the JS code to be registered.$vars
(array (opens new window)) – Array of variables that will be JSON-encoded before being passed to$jsFn
.$position
(integer (opens new window)) – The position at which the JS script tag should be inserted in a page. The possible values are:POS_HEAD (opens new window): in the head section
POS_BEGIN (opens new window): at the beginning of the body section
POS_END (opens new window): at the end of the body section
POS_LOAD (opens new window): enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
POS_READY (opens new window): enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
$key
(string (opens new window), null (opens new window)) – The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.
# registerScript()
Registers a generic <script>
code block.
View source (opens new window)
Arguments
$script
(string (opens new window)) – The generic<script>
code block to be registered$position
(integer (opens new window)) – The position at which the generic<script>
code block should be inserted in a page. The possible values are:- POS_HEAD (opens new window): in the head section
- POS_BEGIN (opens new window): at the beginning of the body section
- POS_END (opens new window): at the end of the body section
$options
(array (opens new window)) – The HTML attributes for the<script>
tag.$key
(string (opens new window), null (opens new window)) – The key that identifies the generic<script>
code block. If null, it will use $script as the key. If two generic<script>
code blocks are registered with the same key, the latter will overwrite the former.
# 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
$category
(string (opens new window)) – The category the messages are in$messages
(string (opens new window)[]) – The messages to be translated
# 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
$template
(string (opens new window)) – The source template string$object
(mixed
) – The object that should be passed into the template$variables
(array (opens new window)) – Any additional variables that should be available to the template$templateMode
(string (opens new window)) – The template mode to use.
Returns
string (opens new window) – The rendered template.
Throws
- yii\base\Exception (opens new window)
in case of failure - Throwable (opens new window)
in case of failure
# renderPageTemplate()
Renders a Twig template that represents an entire web page.
View source (opens new window)
Arguments
$template
(string (opens new window)) – The name of the template to load$variables
(array (opens new window)) – The variables that should be available to the template$templateMode
(string (opens new window), null (opens new window)) – The template mode to use
Returns
string (opens new window) – The rendering result
Throws
\Twig\Error\LoaderError
\Twig\Error\RuntimeError
\Twig\Error\SyntaxError
- yii\base\Exception (opens new window)
if $templateMode is invalid
# renderString()
Renders a template defined in a string.
View source (opens new window)
Arguments
$template
(string (opens new window)) – The source template string.$variables
(array (opens new window)) – Any variables that should be available to the template.$templateMode
(string (opens new window)) – The template mode to use.$escapeHtml
(boolean (opens new window)) – Whether dynamic HTML should be escaped
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
$template
(string (opens new window)) – The name of the template to load$variables
(array (opens new window)) – The variables that should be available to the template$templateMode
(string (opens new window), null (opens new window)) – The template mode to use
Returns
string (opens new window) – The rendering result
Throws
\Twig\Error\LoaderError
\Twig\Error\RuntimeError
\Twig\Error\SyntaxError
- yii\base\Exception (opens new window)
if $templateMode is invalid
# 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
$name
(string (opens new window)) – The name of the template.$templateMode
(string (opens new window), null (opens new window)) – The template mode to use.$publicOnly
(boolean (opens new window)) – Whether to only look for public templates (template paths that don’t start with the private template trigger).
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
$inputName
(string (opens new window))$value
(mixed
)
# setIsDeltaRegistrationActive()
- Since
- 3.4.0
Sets whether delta input name registration is active.
See also registerDeltaName() View source (opens new window)
Arguments
$active
(boolean (opens new window))
# 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
$namespace
(string (opens new window), null (opens new window)) – The new namespace. Set to null to remove the namespace.
# setRegisteredAssetBundles()
- Since
- 3.0.10
Sets the asset bundle names that should be marked as already registered.
View source (opens new window)
Arguments
$names
(string (opens new window)[]) – Asset bundle names
# setRegisteredJsFiles()
- Since
- 3.0.10
Sets the JS files that should be marked as already registered.
View source (opens new window)
Arguments
$keys
(string (opens new window)[])
# 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
$templateMode
(string (opens new window)) – Either 'site' or 'cp'
Throws
- yii\base\Exception (opens new window)
if $templateMode is invalid
# setTemplatesPath()
Sets the base path that templates should be found in.
View source (opens new window)
Arguments
$templatesPath
(string (opens new window))
# 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)
# startHtmlBuffer()
- Since
- 4.3.0
Starts a buffer for any html tags registered with registerHtml().
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
$name
(string (opens new window)) – Name of the bundle to register
# registerAssetFlashes()
Registers any asset bundles and JS code that were queued-up in the session flash data.
View source (opens new window)
Throws
- yii\base\Exception (opens new window)
if any of the registered asset bundles are not actually asset bundles
# 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
$ajaxMode
(boolean (opens new window)) – Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY (opens new window) and POS_LOAD (opens new window) positions will be rendered at the end of the view like normal scripts.
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_CREATE_TWIG
- Type
- craft\events\CreateTwigEvent
- Since
- 4.3.0
The event that is triggered when a Twig environment is created.
See also createTwig()
# EVENT_AFTER_REGISTER_ASSET_BUNDLE
- Type
- craft\events\AssetBundleEvent
- Since
- 4.5.0
The event that is triggered after an asset bundle is registered
# EVENT_AFTER_RENDER_PAGE_TEMPLATE
The event that is triggered after a page template gets rendered
# EVENT_AFTER_RENDER_TEMPLATE
The event that is triggered after a template gets rendered
# EVENT_BEFORE_RENDER_PAGE_TEMPLATE
The event that is triggered before a page template gets rendered
# EVENT_BEFORE_RENDER_TEMPLATE
The event that is triggered before a template gets rendered
# EVENT_REGISTER_CP_TEMPLATE_ROOTS
The event that is triggered when registering control panel template roots
# EVENT_REGISTER_SITE_TEMPLATE_ROOTS
The event that is triggered when registering site template roots
← User AdminTableAsset →