TemplatingExtension.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\Extension;
  3. use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
  6. use Symfony\Bundle\TwigBundle\TokenParser\UrlTokenParser;
  7. use Symfony\Bundle\TwigBundle\TokenParser\PathTokenParser;
  8. use Symfony\Component\Yaml\Dumper as YamlDumper;
  9. /*
  10. * This file is part of the Symfony package.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. /**
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class TemplatingExtension extends \Twig_Extension
  22. {
  23. protected $container;
  24. public function __construct(ContainerInterface $container)
  25. {
  26. $this->container = $container;
  27. }
  28. public function getContainer()
  29. {
  30. return $this->container;
  31. }
  32. public function getTemplating()
  33. {
  34. return $this->container->get('templating.engine');
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getFilters()
  40. {
  41. return array(
  42. 'yaml_encode' => new \Twig_Filter_Method($this, 'yamlEncode'),
  43. 'dump' => new \Twig_Filter_Method($this, 'dump'),
  44. 'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
  45. 'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
  46. 'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
  47. 'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText', array('is_safe' => array('html'))),
  48. 'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))),
  49. 'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))),
  50. 'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))),
  51. );
  52. }
  53. /**
  54. * Returns the token parser instance to add to the existing list.
  55. *
  56. * @return array An array of Twig_TokenParser instances
  57. */
  58. public function getTokenParsers()
  59. {
  60. return array(
  61. // {% javascript 'bundles/blog/js/blog.js' %}
  62. new HelperTokenParser('javascript', '<js> [with <arguments:array>]', 'templating.helper.javascripts', 'add'),
  63. // {% javascripts %}
  64. new HelperTokenParser('javascripts', '', 'templating.helper.javascripts', 'render'),
  65. // {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
  66. new HelperTokenParser('stylesheet', '<css> [with <arguments:array>]', 'templating.helper.stylesheets', 'add'),
  67. // {% stylesheets %}
  68. new HelperTokenParser('stylesheets', '', 'templating.helper.stylesheets', 'render'),
  69. // {% asset 'css/blog.css' %}
  70. new HelperTokenParser('asset', '<location>', 'templating.helper.assets', 'getUrl'),
  71. // {% render 'BlogBundle:Post:list' with ['limit': 2], ['alt': 'BlogBundle:Post:error'] %}
  72. new HelperTokenParser('render', '<template> [with <attributes:array>[, <options:array>]]', 'templating.helper.actions', 'render'),
  73. // {% flash 'notice' %}
  74. new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'getFlash'),
  75. // {% path 'blog_post' with ['id': post.id] %}
  76. new PathTokenParser(),
  77. // {% url 'blog_post' with ['id': post.id] %}
  78. new UrlTokenParser(),
  79. // {% include 'sometemplate.php' with ['something' : 'something2'] %}
  80. new IncludeTokenParser(),
  81. );
  82. }
  83. public function yamlEncode($input, $inline = 0)
  84. {
  85. static $dumper;
  86. if (null === $dumper) {
  87. $dumper = new YamlDumper();
  88. }
  89. return $dumper->dump($input, $inline);
  90. }
  91. public function abbrClass($class)
  92. {
  93. return $this->getTemplating()->get('code')->abbrClass($class);
  94. }
  95. public function abbrMethod($method)
  96. {
  97. return $this->getTemplating()->get('code')->abbrMethod($method);
  98. }
  99. public function formatArgs($args)
  100. {
  101. return $this->getTemplating()->get('code')->formatArgs($args);
  102. }
  103. public function formatArgsAsText($args)
  104. {
  105. return $this->getTemplating()->get('code')->formatArgsAsText($args);
  106. }
  107. public function fileExcerpt($file, $line)
  108. {
  109. return $this->getTemplating()->get('code')->fileExcerpt($file, $line);
  110. }
  111. public function formatFile($file, $line)
  112. {
  113. return $this->getTemplating()->get('code')->formatFile($file, $line);
  114. }
  115. public function formatFileFromText($text)
  116. {
  117. return $this->getTemplating()->get('code')->formatFileFromText($text);
  118. }
  119. public function dump($value)
  120. {
  121. if (is_resource($value)) {
  122. return '%Resource%';
  123. }
  124. if (is_array($value) || is_object($value)) {
  125. return '%'.gettype($value).'% '.$this->yamlEncode($value);
  126. }
  127. return $value;
  128. }
  129. /**
  130. * Returns the name of the extension.
  131. *
  132. * @return string The extension name
  133. */
  134. public function getName()
  135. {
  136. return 'templating';
  137. }
  138. }