TwigEngine.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Bridge\Twig;
  11. use Symfony\Component\Templating\EngineInterface;
  12. use Symfony\Component\Templating\StreamingEngineInterface;
  13. use Symfony\Component\Templating\TemplateNameParserInterface;
  14. /**
  15. * This engine knows how to render Twig templates.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class TwigEngine implements EngineInterface, StreamingEngineInterface
  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. * Streams a template.
  51. *
  52. * @param mixed $name A template name or a TemplateReferenceInterface instance
  53. * @param array $parameters An array of parameters to pass to the template
  54. *
  55. * @throws \RuntimeException if the template cannot be rendered
  56. */
  57. public function stream($name, array $parameters = array())
  58. {
  59. $this->load($name)->display($parameters);
  60. }
  61. /**
  62. * Returns true if the template exists.
  63. *
  64. * @param mixed $name A template name
  65. *
  66. * @return Boolean true if the template exists, false otherwise
  67. */
  68. public function exists($name)
  69. {
  70. if ($name instanceof \Twig_Template) {
  71. return true;
  72. }
  73. $loader = $this->environment->getLoader();
  74. if ($loader instanceof \Twig_ExistsLoaderInterface) {
  75. return $loader->exists($name);
  76. }
  77. try {
  78. $loader->getSource($name);
  79. } catch (\Twig_Error_Loader $e) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * Returns true if this class is able to render the given template.
  86. *
  87. * @param string $name A template name
  88. *
  89. * @return Boolean True if this class supports the given resource, false otherwise
  90. */
  91. public function supports($name)
  92. {
  93. if ($name instanceof \Twig_Template) {
  94. return true;
  95. }
  96. $template = $this->parser->parse($name);
  97. return 'twig' === $template->get('engine');
  98. }
  99. /**
  100. * Loads the given template.
  101. *
  102. * @param mixed $name A template name or an instance of Twig_Template
  103. *
  104. * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
  105. *
  106. * @throws \InvalidArgumentException if the template does not exist
  107. */
  108. protected function load($name)
  109. {
  110. if ($name instanceof \Twig_Template) {
  111. return $name;
  112. }
  113. try {
  114. return $this->environment->loadTemplate($name);
  115. } catch (\Twig_Error_Loader $e) {
  116. throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  117. }
  118. }
  119. }