0
votes

I have a problem with extending templates of my Bundle. Here's the situation: I have my own Bundle in vendors. In my project i use this Bundle, and I wan't to extend some templates ( let's say: /vendor/company/my-bundle/src/Company/MyBundle/Resources/views/Module/index.html.twig). I want to change one block and leave others unchanged. Here's what I did: I created overriding template in the app/Resources/CompanyMyBundle/views/Module/index.html.twig and I extended vendor template: {% extends "CompanyMyBundle:Module:index.html.twig" %}

Problem is that in return I get a "ERR_EMPTY_RESPONSE" error. The template from app overrides vendor and the "CompanyMyBundle:Module:index.html.twig" part refers to the app template, and not the vendor one - so it extends itself. Is there a way, to fix that problem without extending controller and overriding template name in the vendor controller?

2

2 Answers

0
votes

My opinion is that this will not work with the way the template loader works. If there is an override to a vendor template it uses it.

If you want to alter some parts of a template and you own the code of the vendor template, the easiest and I think the right way is to make that part of a code a partial and then override the partial in your application.

{% include '_partial.html' with {'foo': 'bar'} %}

If this is not for any reason possible you need to override the whole template.

0
votes

Extend Symfony Kernel, for access to overridden resources:

<?php
namespace Webility\Bundle\WebilityBundle\HttpKernel;

use Symfony\Component\HttpKernel\Kernel as BaseKernel;

abstract class Kernel extends BaseKernel
{
    public function getBundle($name, $first = true)
    {
        $get_next = false;
        if($name[0] == '!' && $first){
            $name = substr($name, 1);
            $first = false;
            $get_next = true;
        }

        $bundles = parent::getBundle($name, $first);

        if($get_next){
            return isset($bundles[1]) ? $bundles[1] : $bundles[0];
        } else {
            return $bundles;
        }
    }

    public function locateResource($name, $dir = null, $first = true)
    {
        $get_next = false;
        if($name[0] == '@' && $name[1] == '!' && $first){
            $name = '@'.substr($name, 2);
            $first = false;
            $get_next = true;
        }

        $files = parent::locateResource($name, $dir, $first);

        if($get_next){
            return isset($files[1]) ? $files[1] : $files[0];
        } else {
            return $files;
        }
    }
}

Then extend you template this way:

{% extends '!WebilityAdminBundle::layout.html.twig' %}

Exclamation mark means, to use second found result, not first one. PS. don't forget to new Kernel class in app/AppKernel.php