StringLoader.php 1016 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2012 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Extension_StringLoader extends Twig_Extension
  11. {
  12. public function getFunctions()
  13. {
  14. return array(
  15. new Twig_SimpleFunction('template_from_string', 'twig_template_from_string', array('needs_environment' => true)),
  16. );
  17. }
  18. public function getName()
  19. {
  20. return 'string_loader';
  21. }
  22. }
  23. /**
  24. * Loads a template from a string.
  25. *
  26. * <pre>
  27. * {{ include(template_from_string("Hello {{ name }}")) }}
  28. * </pre>
  29. *
  30. * @param Twig_Environment $env A Twig_Environment instance
  31. * @param string $template A template as a string or object implementing __toString()
  32. *
  33. * @return Twig_Template A Twig_Template instance
  34. */
  35. function twig_template_from_string(Twig_Environment $env, $template)
  36. {
  37. return $env->createTemplate((string) $template);
  38. }