In Drupal 8, twig debug mode can be enabled using the documented procedure.
This can break some aspects of the site, which is unfortunate because it is rather confusing to developers if a recommended debug tool changes the behaviour of the site being debugged.
List of known problems
- #2796453: RSS output not valid when using Twig debug
- #2950758: Empty table cells never hidden if twig debug is true and its dupe: #2908634: "Hide if empty" not working if twig debug enabled
- #2656728: Twig split in debug mode
- #2914734: RSS feed view of fields corrupted with twig debug enabled
- #2672656: Remove HTML Debug Comments from a particular field in debug mode
- #2332991: Twig debug and trans fails on HTML attributes
- #3363487: newline on glossify-link.html.twig causes extra space
- #3563677: Twig debug can influence rendering visually due to whitespace between tags
Recommendation
- DONE: Add a warning about this to the documented procedure.
- If twig debug is enabled, add warnings in the Drupal admin UI.
- Where possible, add code conditional on twig debug enabled that fixes the problems. This code could be handled by means of development.services.yml, see also #2839709: Make Debugging Twig easier
Taking the example of RSS views we could:
- For (2) Add a warning to the admin/structure/views/view/%/edit/% for any feed
- For (3) Create new service response_filter.rss.twig_debug linked from development.services.yml and corresponding class
RssResponseTwigDebugFilter implements EventSubscriberInterfacethat fixes the feed by moving the comments after the <xml> tag
Comments
Comment #2
adamps commentedComment #3
adamps commentedComment #4
dawehnerI'm wondering whether we could fix all those issues by somehow just add twig theme suggestions when we actually render HTML.
Not sure how this would be possible, but trying can't hurt :)
Comment #5
dcam commentedI had this issue come up with the Views TimelineJS module this week. The module's Views plugin outputs a JSON object containing the timeline's data to drupalSettings. When Twig debugging is enabled it outputs the debugging info for the data fields into that JSON object, breaking parts of the timeline. A link to that issue is listed here under the "Referenced By" section above.
Comment #12
dalemoore commentedIgnore my response please. (Wish I could delete this comment...)
Comment #16
jwilson3Comment #17
jwilson3Comment #19
jwilson3Comment #20
nick hope commented@jwilson3 The issue you added was already 2nd in the list.
Comment #21
jwilson3Whoops! Thanks for catching that and the cleanup
Comment #23
tgoeg commentedChiming in here and adding another case where this causes problems: #3363487: newline on glossify-link.html.twig causes extra space.
I also had this in a custom theme when checking whether a field is empty (which it was, but not when the debug code inserts newlines).
This does not only cause problems in non-HTML output as the RSS output might imply.
All of my problems got fixed by the following patch and I think it does more good than bad, so I think it could be included in core.
Current firefox and chromium add newlines to comments in developer tools anyway, so this does not really cause any disadvantage for developers, I think, and it really fixes problems on a lot of places (in my case).
Applies against Drupal 10.1
Comment #24
tgoeg commentedComment #25
tgaugesI suggest adding #3563677: Twig debug can influence rendering visually due to whitespace between tags as a child to this issue. It implements a fix similar to the one suggested in #23, implements a new test, and updates existing tests.
I could not find a clear duplicate issue, but please update the issue if you do.
Comment #26
jwilson3