Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

RenderTokenParser.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\View;
  10. use Twig_Node_Expression_Array;
  11. use Twig_Node_Expression_MethodCall;
  12. use Twig_Node_Include;
  13. use Twig_Token;
  14. use Twig_TokenParser;
  15. /**
  16. * Defines a new Twig tag that will render a Piwik View.
  17. *
  18. * Use the tag like this:
  19. *
  20. * {% render theView %}
  21. *
  22. * where `theView` is a variable referencing a View instance.
  23. */
  24. class RenderTokenParser extends Twig_TokenParser
  25. {
  26. /**
  27. * Parses the Twig stream and creates a Twig_Node_Include instance that includes
  28. * the View's template.
  29. *
  30. * @return Twig_Node_Include
  31. */
  32. public function parse(Twig_Token $token)
  33. {
  34. $parser = $this->parser;
  35. $stream = $parser->getStream();
  36. $view = $parser->getExpressionParser()->parseExpression();
  37. $variablesOverride = new Twig_Node_Expression_Array(array(), $token->getLine());
  38. if ($stream->test(Twig_Token::NAME_TYPE, 'with')) {
  39. $stream->next();
  40. $variablesOverride->addElement($this->parser->getExpressionParser()->parseExpression());
  41. }
  42. $stream->expect(Twig_Token::BLOCK_END_TYPE);
  43. $viewTemplateExpr = new Twig_Node_Expression_MethodCall(
  44. $view,
  45. 'getTemplateFile',
  46. new Twig_Node_Expression_Array(array(), $token->getLine()),
  47. $token->getLine()
  48. );
  49. $variablesExpr = new Twig_Node_Expression_MethodCall(
  50. $view,
  51. 'getTemplateVars',
  52. $variablesOverride,
  53. $token->getLine()
  54. );
  55. return new Twig_Node_Include(
  56. $viewTemplateExpr,
  57. $variablesExpr,
  58. $only = false,
  59. $ignoreMissing = false,
  60. $token->getLine()
  61. );
  62. }
  63. /**
  64. * Returns the tag identifier.
  65. *
  66. * @return string
  67. */
  68. public function getTag()
  69. {
  70. return 'render';
  71. }
  72. }