Piwik\
Encapsulates and manages a Twig template.
View lets you set properties that will be passed on to a Twig template. View will also set several properties that will be available in all Twig templates, including:
Template files should be named after the controller method they are used in. If they are used in more than one controller method or are included by another template, they should describe the output they generate and be prefixed with an underscore, eg, _dataTable.twig.
Twig templates must exist in the templates folder in a plugin's root folder.
The following filters are available to twig templates:
{{ 'General_Date'|translate }}
. sprintf parameters can be passed
to the filter.urlRewriteWithParameters: Modifies the current query string with the given set of parameters, eg,
{{ {'module':'MyPlugin', 'action':'index'} | urlRewriteWithParameters }}
sumTime: Pretty formats an number of seconds.
{{ myReallyLongText|truncate(80) }}
implode
.ucwords
.The following functions are available to twig templates:
{{ linkTo({'module':'MyPlugin', 'action':'index'}) }}
.{{ sparkline(sparklineUrl) }}
.{{ postEvent('MyPlugin.event') }}
{% if isPluginLoaded('Goals') %}...{% endif %}
Basic usage
// a controller method
public function myView()
{
$view = new View("@MyPlugin/myView");
$view->property1 = "a view property";
$view->property2 = "another view property";
return $view->render();
}
This class defines the following properties:
$sendHeadersWhenRendering
— Can be disabled to not send headers when rendering a view.$sendHeadersWhenRendering
Can be disabled to not send headers when rendering a view. This can be useful if heaps of views are being rendered during one request to possibly prevent a segmentation fault see eg #15307 . It should not be disabled for a main view, but could be disabled for views that are being rendered eg during a twig event as a "subview" which is part of the "main view".
bool
value.The class defines the following methods:
__construct()
— Constructor.disableCacheBuster()
— Disables the cache buster (adding of ?cb=.getTemplateFile()
— Returns the template filename.getTemplateVars()
— Returns the variables to bind to the template when rendering.__set()
— Directly assigns a variable to the view script.__get()
— Retrieves an assigned variable.__isset()
— Returns true if a template variable has been set or not.__unset()
— Unsets a template variable.render()
— Renders the current view.setContentType()
— Set stored value used in the Content-Type HTTP header field.setXFrameOptions()
— Set X-Frame-Options field in the HTTP response.singleReport()
— Creates a View for and then renders the single report template.getUseStrictReferrerPolicy()
— Returns whether a strict Referrer-Policy header will be sent.setUseStrictReferrerPolicy()
— Sets whether a strict Referrer-Policy header will be sent (if not, nothing is sent).__construct()
Constructor.
$templateFile
(string
) —
The template file to load. Must be in the following format: "@MyPlugin/templateFileName"
. Note the absence of .twig from the end of the name.disableCacheBuster()
Disables the cache buster (adding of ?cb=.
..) to JavaScript and stylesheet files
getTemplateFile()
Returns the template filename.
string
value.getTemplateVars()
Returns the variables to bind to the template when rendering.
$override
(array
) —
Template variable override values. Mainly useful when including View templates in other templates.array
value.__set()
Directly assigns a variable to the view script.
Variable names may not be prefixed with '_'.
$key
(string
) —
The variable name.$val
(mixed
) —
The variable value.__get()
Retrieves an assigned variable.
Variable names may not be prefixed with '_'.
It accepts the following parameter(s):
$key
(string
) —
The variable name.Returns: mixed
—
The variable value.
__isset()
Returns true if a template variable has been set or not.
$name
(string
) —
The name of the template variable.bool
value.__unset()
Unsets a template variable.
$name
(string
) —
The name of the template variable.render()
Renders the current view. Also sends the stored 'Content-Type' HTML header.
See setContentType().
string
—
Serialized data, eg, (image, array, html...).setContentType()
Set stored value used in the Content-Type HTTP header field. The header is set just before rendering.
It accepts the following parameter(s):
$contentType
(string
) —It does not return anything or a mixed result.
setXFrameOptions()
Set X-Frame-Options field in the HTTP response. The header is set just before rendering.
Note: setting this allows you to make sure the View cannot be embedded in iframes. Learn more here.
$option
(string
) —
('deny' or 'sameorigin')singleReport()
Creates a View for and then renders the single report template.
Can be used for pages that display only one report to avoid having to create a new template.
It accepts the following parameter(s):
$title
(string
) —
The report title.$reportHtml
(string
) —
The report body HTML.Returns: string
|void
—
The report contents if $fetch
is true.
getUseStrictReferrerPolicy()
Returns whether a strict Referrer-Policy header will be sent. Generally this should be set to 'true'.
bool
value.setUseStrictReferrerPolicy()
Sets whether a strict Referrer-Policy header will be sent (if not, nothing is sent).
It accepts the following parameter(s):
$useStrictReferrerPolicy
(bool
) —It does not return anything or a mixed result.