Symfony2: how to override core template?

前端 未结 3 1210
心在旅途
心在旅途 2021-02-02 03:38

I am trying to override SymfonyGeneratorBundle templates by creating

\\app\\Resources\\SensioGeneratorBundle\\skeleton\\crud\\views\\index.html.twig
相关标签:
3条回答
  • 2021-02-02 04:09

    The answer of m2mdas worked for me, but only after discovering that it should read

    Filesystem instead of FileSystem!

    Take a look at your vendors/symfony/.../Filesystem folder to verify this.

    0 讨论(0)
  • 2021-02-02 04:10

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

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

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

    0 讨论(0)
  • 2021-02-02 04:19

    To override the edit template, for example, in 2.3+ version, copy the file:

    vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud/views/edit.html.twig.twig
    

    for the directory:

    app/Resources/SensioGeneratorBundle/skeleton/crud/views/edit.html.twig.twig
    

    Now, just generate crud with the default command and it will use the new template.

    0 讨论(0)
提交回复
热议问题