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
78
79
80
81
82
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\View;
use Twig_Node_Expression_Array;
use Twig_Node_Expression_MethodCall;
use Twig_Node_Include;
use Twig_Token;
use Twig_TokenParser;
/**
* Defines a new Twig tag that will render a Piwik View.
*
* Use the tag like this:
*
* {% render theView %}
*
* where `theView` is a variable referencing a View instance.
*/
class RenderTokenParser extends Twig_TokenParser
{
/**
* Parses the Twig stream and creates a Twig_Node_Include instance that includes
* the View's template.
*
* @return Twig_Node_Include
*/
public function parse(Twig_Token $token)
{
$parser = $this->parser;
$stream = $parser->getStream();
$view = $parser->getExpressionParser()->parseExpression();
$variablesOverride = new Twig_Node_Expression_Array(array(), $token->getLine());
if ($stream->test(Twig_Token::NAME_TYPE, 'with')) {
$stream->next();
$variablesOverride->addElement($this->parser->getExpressionParser()->parseExpression());
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$viewTemplateExpr = new Twig_Node_Expression_MethodCall(
$view,
'getTemplateFile',
new Twig_Node_Expression_Array(array(), $token->getLine()),
$token->getLine()
);
$variablesExpr = new Twig_Node_Expression_MethodCall(
$view,
'getTemplateVars',
$variablesOverride,
$token->getLine()
);
return new Twig_Node_Include(
$viewTemplateExpr,
$variablesExpr,
$only = false,
$ignoreMissing = false,
$token->getLine()
);
}
/**
* Returns the tag identifier.
*
* @return string
*/
public function getTag()
{
return 'render';
}
}
|