TwigEngine.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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;
  11. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  12. use Symfony\Component\Templating\TemplateNameParserInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. /**
  16. * This engine knows how to render Twig templates.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class TwigEngine implements EngineInterface
  21. {
  22. protected $environment;
  23. protected $parser;
  24. /**
  25. * Constructor.
  26. *
  27. * @param \Twig_Environment $environment A \Twig_Environment instance
  28. * @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
  29. * @param GlobalVariables $globals A GlobalVariables instance
  30. */
  31. public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser, GlobalVariables $globals)
  32. {
  33. $this->environment = $environment;
  34. $this->parser = $parser;
  35. $environment->addGlobal('app', $globals);
  36. }
  37. /**
  38. * Renders a template.
  39. *
  40. * @param mixed $name A template name
  41. * @param array $parameters An array of parameters to pass to the template
  42. *
  43. * @return string The evaluated template as a string
  44. *
  45. * @throws \InvalidArgumentException if the template does not exist
  46. * @throws \RuntimeException if the template cannot be rendered
  47. */
  48. public function render($name, array $parameters = array())
  49. {
  50. return $this->load($name)->render($parameters);
  51. }
  52. /**
  53. * Returns true if the template exists.
  54. *
  55. * @param mixed $name A template name
  56. *
  57. * @return Boolean true if the template exists, false otherwise
  58. */
  59. public function exists($name)
  60. {
  61. try {
  62. $this->load($name);
  63. } catch (\InvalidArgumentException $e) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. /**
  69. * Returns true if this class is able to render the given template.
  70. *
  71. * @param string $name A template name
  72. *
  73. * @return Boolean True if this class supports the given resource, false otherwise
  74. */
  75. public function supports($name)
  76. {
  77. if ($name instanceof \Twig_Template) {
  78. return true;
  79. }
  80. $template = $this->parser->parse($name);
  81. return 'twig' === $template->get('engine');
  82. }
  83. /**
  84. * Renders a view and returns a Response.
  85. *
  86. * @param string $view The view name
  87. * @param array $parameters An array of parameters to pass to the view
  88. * @param Response $response A Response instance
  89. *
  90. * @return Response A Response instance
  91. */
  92. public function renderResponse($view, array $parameters = array(), Response $response = null)
  93. {
  94. if (null === $response) {
  95. $response = new Response();
  96. }
  97. $response->setContent($this->render($view, $parameters));
  98. return $response;
  99. }
  100. /**
  101. * Loads the given template.
  102. *
  103. * @param mixed $name A template name or an instance of Twig_Template
  104. *
  105. * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
  106. *
  107. * @throws \InvalidArgumentException if the template does not exist
  108. */
  109. protected function load($name)
  110. {
  111. if ($name instanceof \Twig_Template) {
  112. return $name;
  113. }
  114. try {
  115. return $this->environment->loadTemplate($name);
  116. } catch (\Twig_Error_Loader $e) {
  117. throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  118. }
  119. }
  120. }