在Symfony 2中使用Mustache作为模板语言 [英] Using Moustache as a templating language in Symfony 2

查看:69
本文介绍了在Symfony 2中使用Mustache作为模板语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting to use symfony 2 but I'd like to use moustache as the templating language instead of Twig or PHP. I wan't to use moustache because it's totally logicless and because I can also use it in javascript if I decide to handle the rendering of the template clientside.

该怎么做?

推荐答案

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

Some extra info extending @m2mdas answer.

如果您还不熟悉Symfony模板系统和捆绑软件配置,请在开始编码之前先进行以下研究:

If you are not yet familiar with Symfony templating systems and bundle configuration take a look at these before you start coding:

  • How to expose a Semantic Configuration for a Bundle
  • Creating and using Templates
  • How to use PHP instead of Twig for Templates

现在有个快速入门指南.以下面的示例为例,无需坚持使用所选择的名称.

And now a quick recipe to get you started. Take the following as loose examples, no need to stick with the names choosed.

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

1. Create a Resources/config/mustache.xml to define your services and to identify your template engine service (tag it as "templating.engine").

您可以使用Yaml和PHP来代替XML,但是公共"捆绑包更喜欢使用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>

示例:

  • Twig
  • PHP
  • Smarty

2.创建一个Extension类来处理捆绑软件的语义配置.

2. Create an Extension class to handle the semantic configuration for your 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
}

先前类的存在意味着您现在可以在任何配置文件中定义mustache配置名称空间.

The presence of the previous class means that you can now define a mustache configuration namespace in any configuration file.

示例:

  • Twig
  • Smarty

3. [可选] 创建一个Configuration类以验证和合并配置

3. [Optional] Create a Configuration class to validate and merge 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
    }
}

示例:

  • Twig
  • Smarty

4.创建一个实现 EngineInterface

4. Create a MustacheEngine that implements 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;
    }

示例:

  • Twig
  • PHP
  • Smarty

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

# 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));
}

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

You may share a link to your bundle repository so we can track progress and help if needed. Good luck.

这篇关于在Symfony 2中使用Mustache作为模板语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆