Symfony2:如何覆盖核心模板? [英] Symfony2: how to override core template?

查看:27
本文介绍了Symfony2:如何覆盖核心模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过创建来覆盖 SymfonyGeneratorBundle 模板

I am trying to override SymfonyGeneratorBundle templates by creating

\app\Resources\SensioGeneratorBundle\skeleton\crud\views\index.html.twig

该文件应替换:

\vendor\bundles\Sensio\Bundle\GeneratorBundle\Resources\skeleton\crud\views\index.html.twig

但即使在 cache:clear 之后它仍然使用原始文件. 如何在不创建新包的情况下做到这一点,例如 无法覆盖Symfony2 GeneratorBundle 中的标准骨架视图?

But it still uses original file even after cache:clear. How to do that w/o creating new bundle like Can't override the standard skeleton views in Symfony2 GeneratorBundle?

推荐答案

app/AppKernel.php 中的 SensioGeneratorBundle 之后立即注册您的包,例如:

Register your bundle right after SensioGeneratorBundle in app/AppKernel.php e.g.:

// app/AppKernel.php

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
    //.....
    $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    $bundles[] = new Namespace\YourBundle();
}

// Or outside if, should you want your bundle to be available in production environment
$bundles[] = new Namespace\YourBundle();

然后在 YourBundle.php 覆盖 registerCommands 方法,

Then in YourBundle.php override registerCommands method,

// Bundle\YourBundle.php

// other declarations
use Symfony\Component\Console\Application;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
use Symfony\Component\Filesystem\Filesystem;


public function registerCommands(Application $application){
    $crudCommand = $application->get('generate:doctrine:crud');
    $generator = new DoctrineCrudGenerator(new FileSystem, __DIR__.'/Resources/skeleton/crud');
    $crudCommand->setGenerator($generator);

    parent::registerCommands($application);
}

您必须将skeleton 文件夹复制到YourBundle\Resource 并修改模板.

You have to copy skeleton folder to YourBundle\Resource and modify templates.

这篇关于Symfony2:如何覆盖核心模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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