Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

php - Twig keyword `services` always has the value `true`

I'm working in Symfony and tried to pass a variable called $services into my twig template. Naturally, I would also refer to the variable in twig with the same name.

$services = new ArrayCollection();    // Some array

$this->render('template.html.twig', [
    'services' => $services
]);

For some reason however, services always is a boolean value of true. This is instead of the ArrayCollection I am trying to pass into it.


I've checked my twig configuration for anything that could be assigning itself as a global variable but everything checks out, and I can't seem to find any documentation about services being a reserved keyword.

FWIW, I renamed the twig variable name from services to foo for sanity checking and the $services variable behaves as expected, so it's definitely got something to do with assigning a variable to the word services in twig.

I'm currently running Twig 2.14.3

Apart from checking if there are any global variables set, are there any other avenues for trying to find why and where this variable would be defined?

question from:https://stackoverflow.com/questions/65868577/twig-keyword-services-always-has-the-value-true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After a few hours of troubleshooting, it seems like a pretty dumb answer.

I checked:

  • Global variables in my twig.yaml configuration
  • Made sure that any custom twig extensions weren't bound to service
  • Checked that any bound services weren't creating any functions with the name service
  • Made sure that I wasn't setting service in any of my templates up to my base template

It seemed like a whole lotta nothing, then I started turning to some 3rd party bundles that have some twig templates registered.

It turns out a bundle had a template with {% set services = ... %} written inside it, which was included in my base template.


+1 to @yivi for brining debug:twig to my attention, as it was incredibly useful knowing what is bound to twig. It was this info that made me aware of any namespaces that bundles had bound themselves to and gave some extra places to start looking.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...