php - 在 Symfony 2 中使用 Mustache 作为模板语言

标签 php symfony mustache

我开始使用 symfony 2,但我想使用 mustache 作为模板语言而不是 Twig 或 PHP。我不想使用 mustache,因为它完全没有逻辑,而且如果我决定处理模板客户端的呈现,我也可以在 javascript 中使用它。

怎么做?

最佳答案

一些扩展@m2mdas 答案的额外信息。

如果您还不熟悉 Symfony 模板系统和捆绑配置,请在开始编码之前先看看这些:

现在有一个快速入门指南。以下仅作为松散的示例,无需拘泥于所选择的名称。

<强>1。创建一个 Resources/config/mustache.xml 来定义您的服务并标识您的模板引擎服务(将其标记为 "templating.engine")。

您可以使用 Yaml 和 PHP 而不是 XML,但后者更适合“公共(public)”包。

<service id="mustache" class="Mustache">
    <file>Mustache.php</file>
</service>

<service id="templating.engine.mustache" class="MustacheBundle\MustacheEngine" public="false">
        <argument type="service" id="mustache" />
        <argument type="service" id="templating.name_parser"/>
        <argument type="service" id="templating.loader" />
        <tag name="templating.engine" />
</service>

例子:

<强>2。创建一个 Extension 类来处理您的包的语义配置。

<?php

namespace MustacheBundle;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class MustacheExtension extends Extension
{
    $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('mustache.xml');

    // you may parse the $configs array here
    // see: http://symfony.com/doc/current/cookbook/bundles/extension.html#parsing-the-configs-array
}

前一个类的存在意味着您现在可以在任何配置文件中定义 mustache 配置命名空间。

例子:

<强>3。 [可选] 创建一个 Configuration 类来验证和合并配置

<?php

namespace Mustache\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('mustache');

        // see: http://symfony.com/doc/current/cookbook/bundles/extension.html#validation-and-merging-with-a-configuration-class
    }
}

例子:

<强>4。创建一个实现 EngineInterfaceMustacheEngine

<?php

namespace MustacheBundle;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\Loader\LoaderInterface;

use Symfony\Component\HttpFoundation\Response;

class MustacheBundle implements EngineInterface
{
    public function __construct(\Mustache $mustache, TemplateNameParserInterface $parser, LoaderInterface $loader)
    {
        $this->mustache = $mustache;
        $this->parser = $parser;
    }

    public function render($name, array $parameters = array())
    {
        $template = $this->load($name);

        return $this->mustache->render($template);
    }

    // Renders a view and returns a Response.
    public function renderResponse($view, array $parameters = array(), Response $response = null)
    {
        if (null === $response) {
            $response = new Response();
        }

        $response->setContent($this->render($view, $parameters));

        return $response;
    }

    // Returns true if the template exists.
    public function exists($name)
    {
        try {
            $this->load($name);
        } catch (\InvalidArgumentException $e) {
            return false;
        }

        return true;
    }

    // Returns true if this class is able to render the given template.
    public function supports($name)
    {
        $template = $this->parser->parse($name);

        return 'mustache' === $template->get('engine');
    }

    // Loads the given template.
    // Should return the template name or a Mustache template object
    protected function load($name)
    {
        $template = $this->parser->parse($name);
        $template = $this->loader->load($template);

        return (string) $template;
    }

例子:

<强>5。在应用程序配置文件中启用 Shiny 的新模板引擎:

# app/config/config.yml
templating:    { engines: ['twig', 'mustache'] }

6.试试吧

<?php
// src/Acme/HelloBundle/Controller/HelloController.php

public function indexAction($name)
{
    return $this->render('AcmeHelloBundle:Hello:index.html.mustache', array('name' => $name));
}

您可以共享指向您的包存储库的链接,以便我们可以跟踪进度并在需要时提供帮助。祝你好运。

关于php - 在 Symfony 2 中使用 Mustache 作为模板语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965421/

相关文章:

php - date() 或 format_date() 返回 1970?

javascript - symfony框架中的Ajax调用

symfony - 如何处理架构更新时的自定义 sql 函数?

javascript - 是否有一种内置的方法来遍历对象的属性?

php - 创建 SQL 查询

php - lastInsertId() 在数据库中创建 2 条重复记录

用于捕获相关收件人字段中找到的电子邮件地址的 Javascript 和 PHP 正则表达式

symfony - 没有数据库的实体

ruby-on-rails-3.2 - 如何在 Rails 上使用 Rabl with Mustache 和 ruby

javascript - 怎么能逼 Jade 留 mustache ?