LambdaHelper.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2012 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Mustache Lambda Helper.
  12. *
  13. * Passed as the second argument to section lambdas (higher order sections),
  14. * giving them access to a `render` method for rendering a string with the
  15. * current context.
  16. */
  17. class Mustache_LambdaHelper
  18. {
  19. private $mustache;
  20. private $context;
  21. /**
  22. * Mustache Lambda Helper constructor.
  23. *
  24. * @param Mustache_Engine $mustache Mustache engine instance.
  25. * @param Mustache_Context $context Rendering context.
  26. */
  27. public function __construct(Mustache_Engine $mustache, Mustache_Context $context)
  28. {
  29. $this->mustache = $mustache;
  30. $this->context = $context;
  31. }
  32. /**
  33. * Render a string as a Mustache template with the current rendering context.
  34. *
  35. * @param string $string
  36. *
  37. * @return Rendered template.
  38. */
  39. public function render($string)
  40. {
  41. return $this->mustache
  42. ->loadLambda((string) $string)
  43. ->renderInternal($this->context);
  44. }
  45. }