简体   繁体   中英

Inclusion and scope in Twig templates

I have a template child.html.twig that extends parent.html.twig, within the child template, I included a partial _myVar.html.twig which contains a long logic that defines some specific variables.

I got the error : A template that extends another one cannot have a body, I don't understand because the included partial doesdn't output any content, it just defines variables and if its content is just written in child.html.twig the error disappear even outside all blocks.

I wished to put the included partial outside all blocks, but to fix the issue, I included it within one of the overriding blocks.

I got another problem of undefined variable, I don't know why the defined variables within the included partial are not available in the global scope after inclusion.

You should have posted your code, but normally you just have to take care of the following:

  • the extends tag should be the first tag in the template
  • every content in your extendig template has to be defined in blocks (those from the parent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM