TemplatingExtension.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\RenderTokenParser;
  14. /**
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class TemplatingExtension extends \Twig_Extension
  19. {
  20. protected $container;
  21. public function __construct(ContainerInterface $container)
  22. {
  23. $this->container = $container;
  24. }
  25. public function getContainer()
  26. {
  27. return $this->container;
  28. }
  29. public function getTemplating()
  30. {
  31. return $this->container->get('templating');
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getFilters()
  37. {
  38. return array(
  39. 'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
  40. 'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
  41. 'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
  42. 'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText'),
  43. 'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))),
  44. 'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))),
  45. 'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))),
  46. 'file_link' => new \Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))),
  47. );
  48. }
  49. /**
  50. * Returns a list of functions to add to the existing list.
  51. *
  52. * @return array An array of functions
  53. */
  54. public function getFunctions()
  55. {
  56. return array(
  57. 'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
  58. );
  59. }
  60. public function getAssetUrl($location, $packageName = null)
  61. {
  62. return $this->container->get('templating.helper.assets')->getUrl($location, $packageName);
  63. }
  64. /**
  65. * Returns the Response content for a given controller or URI.
  66. *
  67. * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
  68. * @param array $attributes An array of request attributes
  69. * @param array $options An array of options
  70. *
  71. * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
  72. */
  73. public function renderAction($controller, array $attributes = array(), array $options = array())
  74. {
  75. $options['attributes'] = $attributes;
  76. if (isset($options['query'])) {
  77. $options['query'] = $options['query'];
  78. }
  79. return $this->container->get('http_kernel')->render($controller, $options);
  80. }
  81. /**
  82. * Returns the token parser instance to add to the existing list.
  83. *
  84. * @return array An array of Twig_TokenParser instances
  85. */
  86. public function getTokenParsers()
  87. {
  88. return array(
  89. // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
  90. new RenderTokenParser(),
  91. // {% include 'sometemplate.php' with { 'something' : 'something2' } %}
  92. new IncludeTokenParser(),
  93. );
  94. }
  95. public function abbrClass($class)
  96. {
  97. return $this->container->get('templating.helper.code')->abbrClass($class);
  98. }
  99. public function abbrMethod($method)
  100. {
  101. return $this->container->get('templating.helper.code')->abbrMethod($method);
  102. }
  103. public function formatArgs($args)
  104. {
  105. return $this->container->get('templating.helper.code')->formatArgs($args);
  106. }
  107. public function formatArgsAsText($args)
  108. {
  109. return $this->container->get('templating.helper.code')->formatArgsAsText($args);
  110. }
  111. public function fileExcerpt($file, $line)
  112. {
  113. return $this->container->get('templating.helper.code')->fileExcerpt($file, $line);
  114. }
  115. public function formatFile($file, $line, $text = null)
  116. {
  117. return $this->container->get('templating.helper.code')->formatFile($file, $line, $text);
  118. }
  119. public function getFileLink($file, $line)
  120. {
  121. return $this->container->get('templating.helper.code')->getFileLink($file, $line);
  122. }
  123. public function formatFileFromText($text)
  124. {
  125. return $this->container->get('templating.helper.code')->formatFileFromText($text);
  126. }
  127. /**
  128. * Returns the name of the extension.
  129. *
  130. * @return string The extension name
  131. */
  132. public function getName()
  133. {
  134. return 'templating';
  135. }
  136. }