TemplatingExtension.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\Extension;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Bundle\TwigBundle\TokenParser\IncludeTokenParser;
  13. use Symfony\Bundle\TwigBundle\TokenParser\UrlTokenParser;
  14. use Symfony\Bundle\TwigBundle\TokenParser\PathTokenParser;
  15. use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
  16. use Symfony\Component\Yaml\Dumper as YamlDumper;
  17. /**
  18. *
  19. * @author Fabien Potencier <fabien@symfony.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');
  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'),
  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. 'file_link' => new \Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))),
  52. );
  53. }
  54. /**
  55. * Returns a list of functions to add to the existing list.
  56. *
  57. * @return array An array of functions
  58. */
  59. public function getFunctions()
  60. {
  61. return array(
  62. 'url' => new \Twig_Function_Method($this, 'getUrl'),
  63. 'path' => new \Twig_Function_Method($this, 'getPath'),
  64. 'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
  65. );
  66. }
  67. public function getPath($name, array $parameters = array())
  68. {
  69. return $this->container->get('router')->generate($name, $parameters, false);
  70. }
  71. public function getUrl($name, array $parameters = array())
  72. {
  73. return $this->container->get('router')->generate($name, $parameters, true);
  74. }
  75. public function getAssetUrl($location, $packageName = null)
  76. {
  77. return $this->container->get('templating.helper.assets')->getUrl($location, $packageName);
  78. }
  79. /**
  80. * Returns the Response content for a given controller or URI.
  81. *
  82. * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
  83. * @param array $attributes An array of request attributes
  84. * @param array $options An array of options
  85. *
  86. * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
  87. */
  88. public function renderAction($controller, array $attributes = array(), array $options = array())
  89. {
  90. $options['attributes'] = $attributes;
  91. if (isset($options['query'])) {
  92. $options['query'] = $options['query'];
  93. }
  94. return $this->container->get('http_kernel')->render($controller, $options);
  95. }
  96. /**
  97. * Returns the token parser instance to add to the existing list.
  98. *
  99. * @return array An array of Twig_TokenParser instances
  100. */
  101. public function getTokenParsers()
  102. {
  103. return array(
  104. // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
  105. new RenderTokenParser(),
  106. // {% include 'sometemplate.php' with { 'something' : 'something2' } %}
  107. new IncludeTokenParser(),
  108. );
  109. }
  110. public function yamlEncode($input, $inline = 0)
  111. {
  112. static $dumper;
  113. if (null === $dumper) {
  114. $dumper = new YamlDumper();
  115. }
  116. return $dumper->dump($input, $inline);
  117. }
  118. public function abbrClass($class)
  119. {
  120. return $this->container->get('templating.helper.code')->abbrClass($class);
  121. }
  122. public function abbrMethod($method)
  123. {
  124. return $this->container->get('templating.helper.code')->abbrMethod($method);
  125. }
  126. public function formatArgs($args)
  127. {
  128. return $this->container->get('templating.helper.code')->formatArgs($args);
  129. }
  130. public function formatArgsAsText($args)
  131. {
  132. return $this->container->get('templating.helper.code')->formatArgsAsText($args);
  133. }
  134. public function fileExcerpt($file, $line)
  135. {
  136. return $this->container->get('templating.helper.code')->fileExcerpt($file, $line);
  137. }
  138. public function formatFile($file, $line, $text = null)
  139. {
  140. return $this->container->get('templating.helper.code')->formatFile($file, $line, $text);
  141. }
  142. public function getFileLink($file, $line)
  143. {
  144. return $this->container->get('templating.helper.code')->getFileLink($file, $line);
  145. }
  146. public function formatFileFromText($text)
  147. {
  148. return $this->container->get('templating.helper.code')->formatFileFromText($text);
  149. }
  150. public function dump($value)
  151. {
  152. if (is_resource($value)) {
  153. return '%Resource%';
  154. }
  155. if (is_array($value) || is_object($value)) {
  156. return '%'.gettype($value).'% '.$this->yamlEncode($value);
  157. }
  158. return $value;
  159. }
  160. /**
  161. * Returns the name of the extension.
  162. *
  163. * @return string The extension name
  164. */
  165. public function getName()
  166. {
  167. return 'templating';
  168. }
  169. }