HelpersExtension.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\Extension;
  3. use Symfony\Bundle\TwigBundle\TokenParser\HelperTokenParser;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. /*
  6. * This file is part of the Symfony package.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class HelpersExtension extends \Twig_Extension
  18. {
  19. protected $container;
  20. public function __construct(ContainerInterface $container)
  21. {
  22. $this->container = $container;
  23. }
  24. public function getContainer()
  25. {
  26. return $this->container;
  27. }
  28. /**
  29. * Returns the token parser instance to add to the existing list.
  30. *
  31. * @return array An array of Twig_TokenParser instances
  32. */
  33. public function getTokenParsers()
  34. {
  35. return array(
  36. // {% javascript 'bundles/blog/js/blog.js' %}
  37. new HelperTokenParser('javascript', '<js> [with <arguments:array>]', 'templating.helper.javascripts', 'add'),
  38. // {% javascripts %}
  39. new HelperTokenParser('javascripts', '', 'templating.helper.javascripts', 'render'),
  40. // {% stylesheet 'bundles/blog/css/blog.css' with ['media': 'screen'] %}
  41. new HelperTokenParser('stylesheet', '<css> [with <arguments:array>]', 'templating.helper.stylesheets', 'add'),
  42. // {% stylesheets %}
  43. new HelperTokenParser('stylesheets', '', 'templating.helper.stylesheets', 'render'),
  44. // {% asset 'css/blog.css' %}
  45. new HelperTokenParser('asset', '<location>', 'templating.helper.assets', 'getUrl'),
  46. // {% route 'blog_post' with ['id': post.id] %}
  47. new HelperTokenParser('route', '<route> [with <arguments:array>]', 'templating.helper.router', 'generate'),
  48. // {% render 'BlogBundle:Post:list' with ['limit': 2], ['alt': 'BlogBundle:Post:error'] %}
  49. new HelperTokenParser('render', '<template> [with <attributes:array>[, <options:array>]]', 'templating.helper.actions', 'render'),
  50. // {% flash 'notice' %}
  51. new HelperTokenParser('flash', '<name>', 'templating.helper.session', 'getFlash'),
  52. );
  53. }
  54. /**
  55. * Returns the name of the extension.
  56. *
  57. * @return string The extension name
  58. */
  59. public function getName()
  60. {
  61. return 'symfony.helpers';
  62. }
  63. }