View

Type
Class
Namespace
craft\web
Inherits
craft\web\View » yii\web\View » yii\base\View » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable, yii\base\DynamicContentAwareInterface
Since
3.0

View source

Public Properties

PropertyDescription
$assetBundlesyii\web\AssetBundle – List of the registered asset bundles.
$assetManageryii\web\AssetManager – The asset manager.
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$blocksarray – A list of named output blocks.
$bodyHtmlstring – The rendered content
$cacheStackarray, yii\base\DynamicContentAwareInterface – A list of currently active dynamic content class instances.
$contextyii\base\ViewContextInterface – The context under which the renderFile() method is being invoked.
$cpTemplateRootsarray
$cssarray – The registered CSS code blocks.
$cssFilesarray – The registered CSS files.
$defaultExtensionstring – The default view file extension.
$dynamicContentsyii\base\DynamicContentAwareInterface – Class instances supporting dynamic contents.
$dynamicPlaceholdersarray – A list of placeholders for embedding dynamic contents.
$headHtmlstring – The rendered content
$isRenderingPageTemplateboolean – Whether a page template is currently being rendered.
$isRenderingTemplateboolean – Whether a template is currently being rendered.
$jsarray – The registered JS code blocks
$jsFilesarray – The registered JS files.
$linkTagsarray – The registered link tags.
$metaTagsarray – The registered meta tags.
$namespacestring, null – The namespace.
$paramsmixed – Custom parameters that are shared among view templates.
$registeredAssetBundlesstring[] – Asset bundle names
$registeredJsFilesstring[]
$renderersarray – A list of available renderers indexed by their corresponding supported file extensions.
$siteTemplateRootsarray
$templateModestring – Either site or cp.
$templatesPathstring
$themeyii\base\Theme, array, string – The theme object or the configuration for creating the theme object.
$titlestring – The page title
$twigcraft\web\twig\Environment
$viewFilestring, boolean – The view file currently being rendered.

$bodyHtml

Type
string
Access
Read-only

The rendered content

View source

Signature

public string getBodyHtml ( \craft\web\bool $clear = true )

$cpTemplateRoots

Type
array
Access
Read-only

View source

Signature

public array getCpTemplateRoots ( )

$headHtml

Type
string
Access
Read-only

The rendered content

View source

Signature

public string getHeadHtml ( \craft\web\bool $clear = true )

$isRenderingPageTemplate

Type
boolean
Access
Read-only

Whether a page template is currently being rendered.

View source

Signature

public boolean getIsRenderingPageTemplate ( )

$isRenderingTemplate

Type
boolean
Access
Read-only

Whether a template is currently being rendered.

View source

Signature

public boolean getIsRenderingTemplate ( )

$namespace

Type
string, null
Access
Read-only

The namespace.

View source

Signature

public string, null getNamespace ( )

$registeredAssetBundles

Type
string[]
Access
Write-only

Asset bundle names

View source

Signature

public void setRegisteredAssetBundles ( array $names )

$registeredJsFiles

Type
string[]
Access
Write-only

View source

Signature

public void setRegisteredJsFiles ( array $keys )

$siteTemplateRoots

Type
array
Access
Read-only

View source

Signature

public array getSiteTemplateRoots ( )

$templateMode

Type
string

Either site or cp.

View source

Signature

public string getTemplateMode ( )
public void setTemplateMode ( \craft\web\string $templateMode )

$templatesPath

Signature

public string getTemplatesPath ( )
public void setTemplatesPath ( \craft\web\string $templatesPath )

$twig

Signature

public craft\web\twig\Environment getTwig ( )

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__construct()Constructor.
__get()Returns the value of an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to null.
addDynamicPlaceholder()Adds a placeholder for dynamic content.
afterRender()This method is invoked right after renderFile() renders a view file.
afterRenderPageTemplate()Performs actions after a page template is rendered.
afterRenderTemplate()Performs actions after a template is rendered.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
beforeRender()This method is invoked right before renderFile() renders a view file.
beforeRenderPageTemplate()Performs actions before a page template is rendered.
beforeRenderTemplate()Performs actions before a template is rendered.
beginBlock()Begins recording a block.
beginBody()Marks the beginning of an HTML body section.
beginCache()Begins fragment caching.
beginContent()Begins the rendering of content that is to be decorated by the specified view.
beginPage()Marks the beginning of a page.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
clear()Clears up the registered meta tags, link tags, css/js scripts and files.
clearJsBuffer()Clears and ends a JavaScript buffer, returning whatever JavaScript code was included while the buffer was active.
createTwig()Creates a new Twig environment.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
doesTemplateExist()Returns whether a template exists.
endBlock()Ends recording a block.
endBody()Marks the ending of an HTML body section.
endCache()Ends fragment caching.
endContent()Ends the rendering of content.
endPage()Marks the ending of an HTML page.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
evaluateDynamicContent()Evaluates the given PHP statements.
formatInputId()Formats an ID out of an input name.
getAssetManager()Registers the asset manager being used by this view object.
getBehavior()Returns the named behavior object.
getBehaviors()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 CP template roots.
getDynamicContents()Returns a list of currently active dynamic content class instances.
getDynamicPlaceholders()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.
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()
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined.
head()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()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
popDynamicContent()Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances.
pushDynamicContent()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()Registers CSRF meta tags.
registerCss()Registers a CSS code block.
registerCssFile()Registers a CSS file.
registerHiResCss()Registers a hi-res CSS code block.
registerJs()Registers a JS code block.
registerJsFile()Registers a JS file.
registerJsVar()Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.
registerLinkTag()Registers a link tag.
registerMetaTag()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()Renders a view.
renderAjax()Renders a view in response to an AJAX request.
renderDynamic()Renders dynamic content returned by the given PHP statements.
renderFile()Renders a view file.
renderObjectTemplate()Renders an object template.
renderPageTemplate()Renders a Twig template that represents an entire web page.
renderPhpFile()Renders a view file as a PHP script.
renderString()Renders a template defined in a string.
renderTemplate()Renders a Twig template.
renderTemplateMacro()Renders a macro within a given Twig template.
resolveTemplate()Finds a template on the file system and returns its path.
setAssetManager()Sets the asset manager.
setDynamicPlaceholders()Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.
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.
startJsBuffer()Starts a JavaScript buffer.
trigger()Triggers an event.

afterRenderPageTemplate()

Performs actions after a page template is rendered.

View source

Arguments

  • $template (mixed) – The name of the template that was rendered
  • $variables (array) – The variables that were available to the template
  • $output (string) – The template’s rendering result

Signature

public void afterRenderPageTemplate ( \craft\web\string $template, array $variables, \craft\web\string &$output )

afterRenderTemplate()

Performs actions after a template is rendered.

View source

Arguments

  • $template (mixed) – The name of the template that was rendered
  • $variables (array) – The variables that were available to the template
  • $output (string) – The template’s rendering result

Signature

public void afterRenderTemplate ( \craft\web\string $template, array $variables, \craft\web\string &$output )

beforeRenderPageTemplate()

Performs actions before a page template is rendered.

View source

Arguments

  • $template (mixed) – The name of the template to render
  • $variables

Returns

boolean – Whether the template should be rendered

Signature

public boolean beforeRenderPageTemplate ( \craft\web\string $template, array &$variables )

beforeRenderTemplate()

Performs actions before a template is rendered.

View source

Arguments

  • $template (mixed) – The name of the template to render
  • $variables

Returns

boolean – Whether the template should be rendered

Signature

public boolean beforeRenderTemplate ( \craft\web\string $template, array &$variables )

clearJsBuffer()

Clears and ends a JavaScript buffer, returning whatever JavaScript code was included while the buffer was active.

View source

Arguments

  • $scriptTag (boolean) – Whether the JavaScript code should be wrapped in a <script> tag. Defaults to true.

Returns

string, false – The JS code that was included in the active JS buffer, or false if there isn’t one

Signature

public string, false clearJsBuffer ( \craft\web\bool $scriptTag = true )

createTwig()

Creates a new Twig environment.

View source

Returns

craft\web\twig\Environment

Signature

public craft\web\twig\Environment createTwig ( )

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

Arguments

  • $name (string) – The name of the template.

Returns

boolean – Whether the template exists.

Signature

public boolean doesTemplateExist ( \craft\web\string $name )

endBody()

Marks the ending of an HTML body section.

View source

Signature

public void endBody ( )

endPage()

Marks the ending of an HTML page.

View source

Arguments

  • $ajaxMode (boolean) – Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.

Signature

public void endPage ( $ajaxMode = false )

formatInputId()

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

Arguments

  • $inputName (string) – The input name.

Returns

string – The input ID.

Signature

public string formatInputId ( \craft\web\string $inputName )

getBodyHtml()

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

View source

Arguments

  • $clear (boolean) – Whether the content should be cleared from the queue (default is true)

Returns

string – The rendered content

Signature

public string getBodyHtml ( \craft\web\bool $clear = true )

getCpTemplateRoots()

Returns any registered CP template roots.

View source

Returns

array

Signature

public array getCpTemplateRoots ( )

getHeadHtml()

Returns the content to be inserted in the head section. This includes:

View source

Arguments

  • $clear (boolean) – Whether the content should be cleared from the queue (default is true)

Returns

string – The rendered content

Signature

public string getHeadHtml ( \craft\web\bool $clear = true )

getIsRenderingPageTemplate()

Returns whether a page template is currently being rendered.

View source

Returns

boolean – Whether a page template is currently being rendered.

Signature

public boolean getIsRenderingPageTemplate ( )

getIsRenderingTemplate()

Returns whether a template is currently being rendered.

View source

Returns

boolean – Whether a template is currently being rendered.

Signature

public boolean getIsRenderingTemplate ( )

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

Returns

string, null – The namespace.

Signature

public string, null getNamespace ( )

getSiteTemplateRoots()

Returns any registered site template roots.

View source

Returns

array

Signature

public array getSiteTemplateRoots ( )

getTemplateMode()

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

View source

Returns

string – Either site or cp.

Signature

public string getTemplateMode ( )

getTemplatesPath()

Returns the base path that templates should be found in.

View source

Returns

string

Signature

public string getTemplatesPath ( )

getTwig()

Returns the Twig environment.

View source

Returns

craft\web\twig\Environment

Signature

public craft\web\twig\Environment getTwig ( )

hook()

Queues up a method to be called by a given template hook. For example, if you place this in your plugin’s \craft\web\BasePlugin::init() 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.

View source

Arguments

  • $hook (string) – The hook name.
  • $method (callback) – The callback function.

Signature

public void hook ( \craft\web\string $hook, $method )

init()

Initializes the view component.

View source

Signature

public void init ( )

invokeHook()

Invokes a template hook. This is called by \craft\web\HookNode.

View source

Arguments

  • $hook (string) – The hook name.
  • $context

Returns

string – Whatever the hooks returned.

Signature

public string invokeHook ( \craft\web\string $hook, array &$context )

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

Arguments

Returns

string – The namespaced input ID.

Signature

public string namespaceInputId ( \craft\web\string $inputId, \craft\web\string $namespace = null )

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

Arguments

Returns

string – The namespaced input name.

Signature

public string namespaceInputName ( \craft\web\string $inputName, \craft\web\string $namespace = null )

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="title">

would become:

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

View source

Arguments

  • $html (string) – The template with the inputs.
  • $namespace (string, null) – The namespace. Defaults to the active namespace.
  • $otherAttributes (boolean) – Whether id=, for=, etc., should also be namespaced. Defaults to true.

Returns

string – The HTML with namespaced input names.

Signature

public string namespaceInputs ( \craft\web\string $html, \craft\web\string $namespace = null, \craft\web\bool $otherAttributes = true )

normalizeObjectTemplate()

Normalizes an object template for renderObjectTemplate().

View source

Arguments

Returns

string

Signature

public string normalizeObjectTemplate ( \craft\web\string $template )

registerHiResCss()

DEPRECATED

Deprecated in 3.0. Use registerCss() and type your own media selector.

Registers a hi-res CSS code block.

View source

Arguments

  • $css (string) – The CSS code block to be registered
  • $options (array) – The HTML attributes for the 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.

Signature

public void registerHiResCss ( \craft\web\string $css, array $options = [], \craft\web\string $key = null )

registerJs()

Registers a JS code block.

View source

Arguments

  • $js (string) – The JS code block to be registered

  • $position (integer) – The position at which the JS script tag should be inserted in a page. The possible values are:

  • POS_HEAD: in the head section

  • POS_BEGIN: at the beginning of the body section

  • POS_END: at the end of the body section

  • POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.

  • POS_READY: 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) – 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.

Signature

public void registerJs ( $js, $position = self::POS_READY, $key = null )

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

Arguments

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

  • $options (array) – 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: in the head section
    • POS_BEGIN: at the beginning of the body section
    • POS_END: at the end of the body section. This is the default value.

Please refer to yii\helpers\Html::jsFile() for other supported options.

  • $key (string) – 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.

Signature

public void registerJsFile ( $url, $options = [], $key = null )

registerScript()

Registers a generic <script> code block.

View source

Arguments

  • $script (string) – The generic <script> code block to be registered
  • $position (integer) – The position at which the generic <script> code block should be inserted in a page. The possible values are:
  • POS_HEAD: in the head section
  • POS_BEGIN: at the beginning of the body section
  • POS_END: at the end of the body section
  • $options (array) – The HTML attributes for the <script> tag.
  • $key (string) – 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.

Signature

public void registerScript ( $script, $position = self::POS_END, $options = [], $key = null )

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 self::POS_READY.

View source

Arguments

  • $category (string) – The category the messages are in
  • $messages (string[]) – The messages to be translated

Signature

public void registerTranslations ( \craft\web\string $category, array $messages )

registerTwigExtension()

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

View source

Arguments

Signature

public void registerTwigExtension ( Twig_ExtensionInterface $extension )

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, any attributes returned by its fields() or extraFields() methods will also be available as variables to the template.

View source

Arguments

  • $template (string) – The source template string
  • $object (mixed) – The object that should be passed into the template
  • $variables (array) – Any additional variables that should be available to the template

Returns

string – The rendered template.

Throws

Signature

public string renderObjectTemplate ( \craft\web\string $template, $object, array $variables = [] )

renderPageTemplate()

Renders a Twig template that represents an entire web page.

View source

Arguments

  • $template (string) – The name of the template to load
  • $variables (array) – The variables that should be available to the template

Returns

string – The rendering result

Signature

public string renderPageTemplate ( \craft\web\string $template, array $variables = [] )

renderString()

Renders a template defined in a string.

View source

Arguments

  • $template (string) – The source template string.
  • $variables (array) – Any variables that should be available to the template.

Returns

string – The rendered template.

Signature

public string renderString ( \craft\web\string $template, array $variables = [] )

renderTemplate()

Renders a Twig template.

View source

Arguments

  • $template (string) – The name of the template to load
  • $variables (array) – The variables that should be available to the template

Returns

string – The rendering result

Throws

Signature

public string renderTemplate ( \craft\web\string $template, array $variables = [] )

renderTemplateMacro()

Renders a macro within a given Twig template.

View source

Arguments

  • $template (string) – The name of the template the macro lives in.
  • $macro (string) – The name of the macro.
  • $args (array) – Any arguments that should be passed to the macro.

Returns

string – The rendered macro output.

Throws

Signature

public string renderTemplateMacro ( \craft\web\string $template, \craft\web\string $macro, array $args = [] )

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 defaultTemplateExtensions and indexTemplateFilenames 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

Arguments

  • $name (string) – The name of the template.

Returns

string, false – The path to the template if it exists, or false.

Signature

public string, false resolveTemplate ( \craft\web\string $name )

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

Arguments

  • $namespace (string, null) – The new namespace. Set to null to remove the namespace.

Signature

public void setNamespace ( \craft\web\string $namespace = null )

setRegisteredAssetBundles()

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

View source

Arguments

  • $names (string[]) – Asset bundle names

Signature

public void setRegisteredAssetBundles ( array $names )

setRegisteredJsFiles()

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

View source

Arguments

Signature

public void setRegisteredJsFiles ( array $keys )

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 sholud be checked when looking for templates

View source

Arguments

  • $templateMode (string) – Either 'site' or 'cp'

Throws

Signature

public void setTemplateMode ( \craft\web\string $templateMode )

setTemplatesPath()

Sets the base path that templates should be found in.

View source

Arguments

Signature

public void setTemplatesPath ( \craft\web\string $templatesPath )

startJsBuffer()

Starts a JavaScript buffer. JavaScript buffers work similarly to output buffers in PHP. Once you’ve started a JavaScript buffer, any JavaScript code included with registerJs() will be included in a buffer, and you will have the opportunity to fetch all of that code via clearJsBuffer() without having it actually get output to the page.

View source

Signature

public void startJsBuffer ( )

Protected Methods

MethodDescription
findViewFile()Finds the view file based on the given view name.
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 once files are registered.

View source

Signature

protected void registerAllAssetFiles ( )

registerAssetFiles()

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

Removes a bundle from $assetBundles once files are registered.

View source

Arguments

  • $name (string) – Name of the bundle to register

Signature

protected void registerAssetFiles ( $name )

registerAssetFlashes()

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

View source

Throws

Signature

protected void registerAssetFlashes ( )

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

Returns

string – The rendered content

Signature

protected string renderBodyBeginHtml ( )

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

Arguments

  • $ajaxMode (boolean) – Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.

Returns

string – The rendered content

Signature

protected string renderBodyEndHtml ( $ajaxMode )

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

Returns

string – The rendered content

Signature

protected string renderHeadHtml ( )

Constants

ConstantDescription
PH_BODY_BEGINThis is internally used as the placeholder for receiving the content registered for the beginning of the body section.
PH_BODY_ENDThis is internally used as the placeholder for receiving the content registered for the end of the body section.
PH_HEADThis is internally used as the placeholder for receiving the content registered for the head section.
POS_BEGINThe location of registered JavaScript code block or files.
POS_ENDThe location of registered JavaScript code block or files.
POS_HEADThe location of registered JavaScript code block or files.
POS_LOADThe location of registered JavaScript code block.
POS_READYThe 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 CP template roots

EVENT_REGISTER_SITE_TEMPLATE_ROOTS

Type
craft\events\RegisterTemplateRootsEvent

The event that is triggered when registering site template roots