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

Nic*_*tti 11 php symfony mustache

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

怎么做?

noi*_*eed 29

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

如果您还不熟悉Symfony模板系统和软件包配置,请在开始编码之前先看看它们:

现在是一个快速的方法,让你开始.以下是松散的例子,不需要坚持选择的名字.

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

您可以使用Yaml和PHP而不是XML,但后者更适合"公共"捆绑.

<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>
Run Code Online (Sandbox Code Playgroud)

例子:

2.创建一个Extension类来处理bundle的语义配置.

<?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
}
Run Code Online (Sandbox Code Playgroud)

前一个类的存在意味着您现在可以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
    }
}
Run Code Online (Sandbox Code Playgroud)

例子:

4.创建一个MustacheEngine实现EngineInterface的方法

<?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;
    }
Run Code Online (Sandbox Code Playgroud)

例子:

5.在应用程序配置文件中启用闪亮的新模板引擎:

# app/config/config.yml
templating:    { engines: ['twig', 'mustache'] }
Run Code Online (Sandbox Code Playgroud)

6.试一试

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

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

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


Mun*_*Das 5

您必须创建一个实现EngineInterface的类,并创建一个名为templating.engine.mustache引用该类的DIC服务.然后在app/config.yml你可以设置默认引擎.

#app/config.yml
framework:
  #.....
  templating:
      engines: ['mustache'] //mustache is the last portion of the service id 
Run Code Online (Sandbox Code Playgroud)

作为参考,您可以检查PhpEngine类及其服务定义.