加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用胡子作为Symfony 2中的模板语言

发布时间:2020-12-13 13:14:52 所属栏目:PHP教程 来源:网络整理
导读:我开始使用symfony 2,但我想使用胡子作为模板语言,而不是Twig或 PHP.我不会使用胡子,因为它是完全无逻辑的,因为我也可以在javascript中使用它,如果我决定处理模板客户端的渲染. 任何建议如何做到这一点? 一些额外的信息延伸@ m2mdas答案. 如果您还不熟悉Sym
我开始使用symfony 2,但我想使用胡子作为模板语言,而不是Twig或 PHP.我不会使用胡子,因为它是完全无逻辑的,因为我也可以在javascript中使用它,如果我决定处理模板客户端的渲染.

任何建议如何做到这一点?

一些额外的信息延伸@ m2mdas答案.

如果您还不熟悉Symfony模板系统和软件包配置,请先开始编写代码:

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

现在是一个快速的食谱让你开始.以下列为松散的例子,不需要坚持选择的名称.

1.创建一个Resources / config / mustache.xml来定义您的服务,并确定您的模板引擎服务(将其标记为“templating.engine”).

您可以使用Yaml和PHP而不是XML,但后者更适用于“public”bundle.

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

<service id="templating.engine.mustache" class="MustacheBundleMustacheEngine" 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类来处理你的bundle的语义配置.

<?php

namespace MustacheBundle;

use SymfonyComponentHttpKernelDependencyInjectionExtension;
use SymfonyComponentDependencyInjectionLoaderXmlFileLoader;

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
}

上一个类的存在意味着您现在可以在任何配置文件中定义一个胡塞配置命名空间.

例子:

> Twig
> Smarty

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

<?php

namespace MustacheDependencyInjection;

use SymfonyComponentConfigDefinitionBuilderTreeBuilder;
use SymfonyComponentConfigDefinitionConfigurationInterface;

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的MustacheEngine

<?php

namespace MustacheBundle;

use SymfonyBundleFrameworkBundleTemplatingEngineInterface;
use SymfonyComponentTemplatingTemplateNameParserInterface;
use SymfonyComponentTemplatingLoaderLoaderInterface;

use SymfonyComponentHttpFoundationResponse;

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'] }

尝试一下

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

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

您可以分享到您的软件包存储库的链接,以便我们可以跟踪进度和需要时的帮助.祝你好运.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读