masked82/mustache

PHP 中 Mustache 的实现。

v2.5.0 2013-12-14 20:57 UTC

README

PHP 中 Mustache 的实现。

Build Status

新增功能

视图辅助函数现在可以访问上下文对象

<?php
$mustache = new Mustache_Engine(
    array(
        'helpers' => array(
            'displayName' => function($source, \Mustache_LambdaHelper $lambdaHelper, \Mustache_Context $context) {
                // Access the current context:
                $contextValue = $context->last();
                return isset($contextValue->name) ? $contextValue->name : '';
            },
            'changeName' => function($source, \Mustache_LambdaHelper $lambdaHelper, \Mustache_Context $context) {
                // Remove the current context:
                $contextValue = $context->pop();
                // Change the context value:
                $contextValue->name = 'New Name';
                // Save the context value
                $context->push($contextValue);
                return $lambdaHelper->render($source);
            },
        ),
    )
);

使用方法

<?php
echo $mustache->render(
    'Name:
    {{#person}}
        <p>
            {{#displayName}}{{/displayName}}
        </p>
        <p>
            {{#changeName}}
                The name is changed, but the last name is still {{lastName}}.
            {{/changeName}}
        </p>
        <p>
            {{#displayName}}{{/displayName}}
        </p>
    {{/person}}',
    array(
        'person' => array(
            'name' => 'Some Name',
            'lastName' => 'Last Name'
        )
    )
);

结果将是

Name:

Some Name
The name is changed, but the last name is still Last Name.
New Name

这还不算全部!

阅读 Mustache.php 文档 了解更多信息。