TwigEngine.php 3.6KB

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