APY / APYDataGridBundle

Symfony Datagrid Bundle
MIT License
492 stars 344 forks source link

"_self template“ doesn't work. #1022

Closed slince closed 6 years ago

slince commented 6 years ago

This is my template:

{% extends 'AcmeBundle::layout.html.twig' %}

{% block pageHeader %}
<h1>
    {{ 'admin.user.management'|trans }}
    <small>{{ 'admin.user.index'|trans }}</small>
</h1>
{% endblock %}

{% block content %}
    {{ grid(grid, _self) }}
{% endblock %}

{% block grid_filters %}
{% endblock %}

error:

An exception has been thrown during the rendering of a template ("Block "grid" doesn't exist in grid template "AcmeBundle:User:index.html.twig".").

it does not work like the follow:

If you want to override blocks inside the current template you can use the _self parameter in grid template definition. Current template will automatically extended from the base block template (APYDataGridBundle::blocks.html.twig)

Looking forward to your reply.

DonCallisto commented 6 years ago

Well, what is 'AcmeBundle::layout.html.twig? Maybe you're not extending the right templates?

slince commented 6 years ago

AcmeBundle::layout.html.twig is a basic layout template. like


<html>
<head>admin</head>
<body>
<div class="content-wrapper">
        <section class="content-header">
            {% block pageHeader %}{% endblock %}
            {{ breadcrumb(admin_breadcrumb)|raw }}
        </section>
        <section class="content">
        {% block content %}{% endblock %}
        </section>
    </div>
</body>
DonCallisto commented 6 years ago

Is the first template overriding the bundle one? Could you tell me the name of first template and the path? Thanks.

slince commented 6 years ago

Is the first template overriding the bundle one

No,

this is my controller code.

    /**
     * 用户管理列表
     *
     * @Route("/users", name="admin_user_index")
     */
    public function indexAction()
    {
        $this->breadcrumb->push('admin.user.index');
        $grid = $this->gridFactory->get(User::class);

        return $grid->getGridResponse('AcmeBundle:User:index.html.twig');
    }
DonCallisto commented 6 years ago

Ok, so if it is not overriding, how do you expect this to work? That's not an issue with the bundle but an issue with your code.

I'm closing this.

slince commented 6 years ago

hi, can you tell me how to do so it can work.

DonCallisto commented 6 years ago

Sorry, my fault, you don't need to extend or override anything. It seems like the DataGridExtension isn't registered. Weird.

slince commented 6 years ago

DataGridExtension has been registered. it can work using default theme.

...
{{ grid(grid) }}
...
slince commented 6 years ago

image

DonCallisto commented 6 years ago

So I think it's not an issue of this bundle

slince commented 6 years ago

ok, can you tell me some core code about "self template". so i can debug it by myself.

DonCallisto commented 6 years ago

You can view all the logic starting from DataGridExtension

slince commented 6 years ago

I think it's because beacause the _self variable return the current template name, not the template instance from twig 2.0.0

slince commented 6 years ago

Hi,

I am a little confused. i don't know why getTemplatesFromString need to find parents of the template.

i think it's not necessary, you know, blocks in the parent template can be overwritten by the current template.

DonCallisto commented 6 years ago

Sorry, ATM I'm not able to take a look at this due to my little free time and due to the fact I'm the only devel that's following the project but I didn't wrote it. If you want to submit a PR, you're welcome but I really don't know if and when I'll have the time to look and eventually merge it.

slince commented 6 years ago

Hi, i have expanded the class DataGridExtension and rewritten method getTemplates like this:

    protected function getTemplates(Twig_Environment $environment)
    {
        if ($this->templates) {
            return $this->templates;
        }
        $this->templates[] = $environment->load($this->defaultTemplate);
        if ($this->theme) {
            if (!$this->theme instanceof \Twig_Template && !$this->theme instanceof \Twig_TemplateWrapper) {
                $template = $environment->load($this->theme);
            } else {
                $template = $this->theme;
            }
arrau
            array_unshift($this->templates, $template);
        }
        return $this->templates;
    }

and it works now for me. if you think this is ok, i will submit a pr. 😃