2012-04-01 36 views
10

我開始使用symfony 2,但是我想使用鬍子作爲模板語言而不是Twig或PHP。我不想使用小鬍子,因爲它完全沒有邏輯性,因爲如果我決定處理模板客戶端的渲染,我也可以在javascript中使用它。在Symfony中使用鬍子作爲模板語言2

有關如何做到這一點的任何建議?

+0

我一直想要完成這一段時間... https://github.com/bobthecow/BobthecowMustacheBundle隨意叉和幫助。 – bobthecow 2012-04-03 02:11:27

+0

@bobthecow我分叉它,我是相當新的symfony2,我想我會花一些時間來理解一切,特別是因爲這是在我的業餘時間,無論如何,謝謝! :) – 2012-04-03 21:41:15

+0

截至最新的提交,我的包與Symfony2 v2.1.0-DEV :) – bobthecow 2012-05-18 04:54:12

回答

28

一些額外的信息延伸@ 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> 

實例:

2.創建Extension類來處理y的語義構造我們的捆綁。

<?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.創建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; 
    } 

實例:

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

您可以共享一個鏈接到您的軟件包存儲庫,以便我們可以跟蹤進度並在需要時提供幫助。祝你好運。

+0

謝謝,這是一個非常好的答案,我會盡快寫一些代碼(我使用simfony2爲我的個人項目!):) – 2012-04-02 12:16:05

+1

noisebleed:偉大的writeup。它幫助了一噸:) - https://github.com/bobthecow/BobthecowMustacheBundle – bobthecow 2012-05-18 22:17:32

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 

僅供參考,您可以檢查PhpEngine類及其service definition

+1

這。我有一個80%完成...我可以發佈它,如果你想玩玩:) – bobthecow 2012-04-03 01:53:19