mustache/mustache

PHP中的Mustache实现。

v2.14.2 2022-08-23 13:07 UTC

README

PHP中的Mustache实现。

Package version Monthly downloads

安装

composer require mustache/mustache

用法

快速示例

<?php
$m = new Mustache_Engine(array('entity_flags' => ENT_QUOTES));
echo $m->render('Hello {{planet}}', array('planet' => 'World!')); // "Hello World!"

更深入的示例 -- 这是标准的Mustache模板

Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}

创建一个"视图"的上下文对象 -- 这也可以是一个关联数组,但它们并不擅长处理函数

<?php
class Chris {
    public $name  = "Chris";
    public $value = 10000;

    public function taxed_value() {
        return $this->value - ($this->value * 0.4);
    }

    public $in_ca = true;
}

并渲染它

<?php
$m = new Mustache_Engine(array('entity_flags' => ENT_QUOTES));
$chris = new Chris;
echo $m->render($template, $chris);

注意:我们建议将entity_flags作为默认值使用ENT_QUOTES,以降低跨站脚本攻击的风险。

还有更多内容!

阅读Mustache.php文档以获取更多信息。

另请参阅