sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Twig variables appear to be out of scope in over-ridden Sonata template #1958

Closed dnagirl closed 8 years ago

dnagirl commented 10 years ago

I am trying to override the default standard_layout.html.twig file from the SonataAdminBundle.The new version is in app/Resources/SonataAdmin/views. It extends ::base.html.twig. Essentially, I have cut and pasted Sonata blocks into the appropriate blocks in my base template. This works fine on the main dashboard. But as soon as I click a link to list or add any entity, I get an error indicating that one of the variables declared at the top of standard_layout.html.twig is undefined in standard_layout.html.twig. If I move the {% set _breadcrumb = block('breadcrumb' %} declaration (for example) from the top of the file to the line just before the variable is used, the error goes away and the next called variable causes a similar error.

If I declare my own variables, all child templates have access to them. It's just the sonata variables that are no longer available.

//standard_layout.twig.html
{% extends '::base.html.twig' %}

{% set myvarworks = block('not_a_block_name') %}

//HERE ARE THE PROBLEM VARIABLES   
{% set _preview      = block('preview') %}
{% set _form         = block('form') %}
{% set _show         = block('show') %}
{% set _list_table   = block('list_table') %}
{% set _list_filters = block('list_filters') %}
{% set _side_menu    = block('side_menu') %}
{% set _content      = block('content') %}
{% set _title        = block('title') %}
{% set _breadcrumb   = block('breadcrumb') %}
{% set _actions      = block('actions') %}

//dashboard.html.twig
{% dump(myvarworks)%}   // empty string
{% dump(_breadcrumb) %} //throws Exception: Variable "_breadcrumb" does not exist

When I look at the block profiler, I notice 2 differences between using the default standard_layout.html.twig and mine:

Why can't child templates inherit sonata variable from the replacement template? And why are the blocks loaded twice in the replacement template?

Here is a gist of my standard_layout.html.twig file.

camdarley commented 9 years ago

Hi, Did you find a solution to this? I know this post is more than one year old but I have exactly the same issue.

dnagirl commented 9 years ago

Sorry no. I decided that Sonata wasn't a good solution for that project.