2

我遇到了胡子模板的问题,希望有人能提供帮助。具体来说,如果我将部分作为数据项嵌入到我的模板中,Mustache 不会完全解析我的模板。如何让 Mustache 完全解析所有模板,包括作为数据的部分?

<?php
$m = new Mustache;

$template = '
    {{header}}
    {{body}}
    {{footer}}
';

$data = array(
    'header' => 'header', 
    'body' => '{{> embedded}}', 
    'footer' => 'footer'
);

$partials = array(
    'embedded' => 'embedded'
);

die($m->render($template, $data, $partials));
?>

我期望看到的是:

header embedded footer

但实际发生的是

header {{> embedded}} footer

如果我{{> embedded}}直接放入模板中它可以工作,但是由于某种原因,我现在无法对该值进行硬编码。我也不能使用特定于 php 的解决方案,因为模板需要在客户端与 javascript 一样良好地运行。

4

2 回答 2

2

Mustache is "completely parsing" your templates. It's just not double-parsing them. In fact, Mustache takes specific pains not to double-parse your templates. Doing that would create an opportunity for mustache injection (like SQL injection, but for your templates). This is a Bad Thing :)

It's possible to accomplish what you're looking for with higher-order sections, but that would require some code. That said, it shouldn't be too tough to write both a PHP and JavaScript implementation.

于 2012-04-25T10:32:18.187 回答
0

这种方式就行了。你怎么看?

    $partials = array(
        'embedded' => 'embedded'
    );

    $m = new Mustache_Engine(array('partials' => $partials));

    $template = '
        {{header}}
        {{>embedded}}
        {{footer}}
    ';

    $data = array(
        'header' => 'header', 
        'footer' => 'footer'
    );

    die($m->render($template, $data, $partials));
于 2012-10-05T21:52:44.823 回答