1<?php
2
3/*
4 * This file is part of Mustache.php.
5 *
6 * (c) 2010-2017 Justin Hileman
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * Mustache Lambda Helper.
14 *
15 * Passed as the second argument to section lambdas (higher order sections),
16 * giving them access to a `render` method for rendering a string with the
17 * current context.
18 */
19class Mustache_LambdaHelper
20{
21    private $mustache;
22    private $context;
23    private $delims;
24
25    /**
26     * Mustache Lambda Helper constructor.
27     *
28     * @param Mustache_Engine  $mustache Mustache engine instance
29     * @param Mustache_Context $context  Rendering context
30     * @param string           $delims   Optional custom delimiters, in the format `{{= <% %> =}}`. (default: null)
31     */
32    public function __construct(Mustache_Engine $mustache, Mustache_Context $context, $delims = null)
33    {
34        $this->mustache = $mustache;
35        $this->context  = $context;
36        $this->delims   = $delims;
37    }
38
39    /**
40     * Render a string as a Mustache template with the current rendering context.
41     *
42     * @param string $string
43     *
44     * @return string Rendered template
45     */
46    public function render($string)
47    {
48        return $this->mustache
49            ->loadLambda((string) $string, $this->delims)
50            ->renderInternal($this->context);
51    }
52
53    /**
54     * Render a string as a Mustache template with the current rendering context.
55     *
56     * @param string $string
57     *
58     * @return string Rendered template
59     */
60    public function __invoke($string)
61    {
62        return $this->render($string);
63    }
64
65    /**
66     * Get a Lambda Helper with custom delimiters.
67     *
68     * @param string $delims Custom delimiters, in the format `{{= <% %> =}}`
69     *
70     * @return Mustache_LambdaHelper
71     */
72    public function withDelimiters($delims)
73    {
74        return new self($this->mustache, $this->context, $delims);
75    }
76}
77