Pipe\JstProcessor::render PHP Method

render() public method

public render ( $context = null, $vars = [] )
    function render($context = null, $vars = array())
    {
        $namespace = static::$defaultNamespace;
        $logicalPath = $context->environment->logicalPath($context->path);
        $basename = substr($logicalPath, 0, strpos($logicalPath, '.'));
        $name = json_encode($basename);
        # Indent with four spaces
        $value = preg_replace('/$(.)/m', '\\1    ', $this->getData());
        if (@$this->options['quote']) {
            # Escape quotes, and quote data
            $value = sprintf('"%s"', str_replace('"', '\\"', $value));
        }
        return <<<JST
(function() {
    {$namespace} || ({$namespace} = {});

    {$namespace}[{$name}] = {$value};
}).call(this);
JST;
    }

Usage Example

Example #1
0
    function testQuoting()
    {
        $this->ctx->logicalPath = 'foo';
        $jst = new JstProcessor(function () {
            return <<<JS
{{#foo}}<span>foo</span>{{/foo}} "foo"
JS;
        }, array('quote' => true));
        $this->assertEquals(<<<EXPECTED
(function() {
    this.JST || (this.JST = {});

    this.JST["foo"] = "{{#foo}}<span>foo</span>{{/foo}} \\"foo\\"";
}).call(this);
EXPECTED
, $jst->render($this->ctx));
    }