blob: dca1dee05ef0ae7ed07f2b0ccf9f58478535480f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Widget;
use Piwik\View;
/**
* Defines a new widget. You can create a new widget using the console command `./console generate:widget`.
* The generated widget will guide you through the creation of a widget.
*
* For an example, see {@link https://github.com/piwik/piwik/blob/master/plugins/ExamplePlugin/Widgets/MyExampleWidget.php}
*
* @api since Piwik 3.0.0
*/
class Widget
{
/**
* @param WidgetConfig $config
* @api
*/
public static function configure(WidgetConfig $config)
{
}
/**
* @return string
*/
public function render()
{
return '';
}
/**
* Assigns the given variables to the template and renders it.
*
* Example:
*
* public function myControllerAction () {
* return $this->renderTemplate('index', array(
* 'answerToLife' => '42'
* ));
* }
*
* This will render the 'index.twig' file within the plugin templates folder and assign the view variable
* `answerToLife` to `42`.
*
* @param string $template The name of the template file. If only a name is given it will automatically use
* the template within the plugin folder. For instance 'myTemplate' will result in
* '@$pluginName/myTemplate.twig'. Alternatively you can include the full path:
* '@anyOtherFolder/otherTemplate'. The trailing '.twig' is not needed.
* @param array $variables For instance array('myViewVar' => 'myValue'). In template you can use {{ myViewVar }}
* @return string
* @api
*/
protected function renderTemplate($template, array $variables = array())
{
if (false === strpos($template, '@') || false === strpos($template, '/')) {
$aPluginName = explode('\\', get_class($this));
$aPluginName = $aPluginName[2];
$template = '@' . $aPluginName . '/' . $template;
}
$view = new View($template);
foreach ($variables as $key => $value) {
$view->$key = $value;
}
return $view->render();
}
}
|