New line at the end of some template files cauzes empty regions to be rendered.

Created on 12 May 2024, about 2 months ago
Updated 17 June 2024, 11 days ago

Recently we have updated the USWDS Base theme from v3.3.0 to v3.7.1. After the update, the first sidebar region is being rendered even if there is no content in it. Before the update the region was not rendered when it did not contain any renderable data.

Being aware that there are different techniques for checking empty content, and that empty render elements could still trigger false positives when checked for emptiness, I went and double checked if we had any changes introduced related to the empty check in our own custom subtheme. Turned out no changes were introduced and the region was rendered via the base theme template.

After debugging this issue for a bit, and looking into twig engine, I discovered that region--sidebar_first.html.twig template, while not having any renderable data in the {{ content }}, still renders as a not empty string, and the only thing it contains is the new line character - "\n".

I combed through the change log and found that changes applied in Issue 3363297: Fix the issues reported by phpcs + add gitlab πŸ“Œ Fix the issues reported by phpcs + add gitlab Fixed added a trailing new line to a bunch of template files.

It is hard for me to say whether this is Twig engine bug (should one expect Twig to discard the last new line character in the file), or is it expected behavior, and then the "newline at the end of file" coding standards should not be applied to the template files - IDK. But the issue can be addressed by one of the two options below:

1. By wrapping {{ content }} into {% if %} statement,
2. Print content with white space control - {{- content -}}

Both seem to solve the problem.

πŸ› Bug report
Status

Fixed

Version

3.7

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States euk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024