StringLoader.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Drupal\Core\Template\Loader;
  3. /**
  4. * Loads string templates, also known as inline templates.
  5. *
  6. * This loader is intended to be used in a Twig loader chain and whitelists
  7. * string templates that begin with the following comment:
  8. * @code
  9. * {# inline_template_start #}
  10. * @endcode
  11. *
  12. * This class override ensures that the string loader behaves as expected in
  13. * the loader chain. If Twig's string loader is used as is, any string (even a
  14. * reference to a file-based Twig template) is treated as a valid template and
  15. * is rendered instead of a \Twig_Error_Loader exception being thrown.
  16. *
  17. * @see \Drupal\Core\Template\TwigEnvironment::renderInline()
  18. * @see \Drupal\Core\Render\Element\InlineTemplate
  19. * @see twig_render_template()
  20. */
  21. class StringLoader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function exists($name) {
  26. if (strpos($name, '{# inline_template_start #}') === 0) {
  27. return TRUE;
  28. }
  29. else {
  30. return FALSE;
  31. }
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getSource($name) {
  37. return $name;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getCacheKey($name) {
  43. return $name;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function isFresh($name, $time) {
  49. return TRUE;
  50. }
  51. }