Skip to main content

Update Drupal to 10.1.x and I'm receiving the following error

Twig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "macro" tag defined near line 2). in Twig\Parser->subparse() (line 2 of themes/custom/{theme}/templates/misc/filesize-macro.html.twig).

The error message clearly indicates that there's a problem in the Twig template file at line 2. Specifically, there is an "Unexpected 'spaceless' tag" and it's expecting a closing tag for the "macro" tag that was defined near line 2.

Let's break down the error message and address the issue:

Unexpected 'spaceless' tag

The 'spaceless' tag in Twig is used to remove whitespace and line breaks between HTML tags, but it must be properly enclosed within a macro or block. It seems like you have an issue with the placement of the 'spaceless' tag.

Expecting closing tag for the "macro" tag defined near line 2: This part of the error message indicates that the 'macro' tag defined near line 2 is not properly closed or there might be an issue with the macro definition.

To resolve this issue, you should review the Twig template (filesize-macro.html.twig) and ensure that the 'spaceless' tag is properly enclosed within a macro or block. It should be used like this:

{% macro yourMacroName() %}
   {# Your content here #}
   {% spaceless %}
       {# Content you want to be spaceless #}
   {% endspaceless %}
   {# More content here #}
{% endmacro %}

How to review:

  • Check that any 'macro' or 'block' tags in your template are correctly opened and closed, and there are no syntax errors within these macro or block definitions.
  • Verify the entire template for any missing or mismatched opening and closing tags, including other macros or blocks in the template.
  • Make sure that there are no unexpected tags or syntax errors in the template file that could be causing this issue.

Comparing the above outline with the actual code

{% macro bytesToSize(bytes) %}
	{% spaceless %}
    	{% set kilobyte = 1024 %}
    	{% set megabyte = kilobyte * 1024 %}
    	{% set gigabyte = megabyte * 1024 %}
    	{% set terabyte = gigabyte * 1024 %}
    	{% if bytes < kilobyte %}
        	{{ bytes ~ ' B' }}
    	{% elseif bytes < megabyte %}
        	{{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
    	{% elseif bytes < gigabyte %}
        	{{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
    	{% elseif bytes < terabyte %}
        	{{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
    	{% else %}
        	{{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
    	{% endif %}
	{% endspaceless %}
{% endmacro %}

 

Spaceless tag deprecated

The spaceless tag was deprecated in Twig 1.  It is now applied as a filter or using the apply tag.

{{ "<div>
       <strong>Content you want to be spaceless</strong>
   </div>"|spaceless }}

Generates the following output

<div><strong>Content you want to be spaceless</strong></div>

You can combine spaceless with the apply tag to apply the transformation on large amounts of HTML:

{% apply spaceless %}
   <div>
       <strong>Content you want to be spaceless</strong>
   </div>
{% endapply %}

The output will be the same as noted above.

 

Change required

Use the apply tag with spaceless so the open tag changes from {% spaceless %} to  {% apply spaceless %}.  Subsequently, the close tag is also impacted transitioning from endspaceless to endapply {% endapply %}.

{% macro bytesToSize(bytes) %}
   {% apply spaceless %}
       {# Content you want to be spaceless #}
   {% endapply %}
{% endmacro %}

 

Apply solution applied

{% macro bytesToSize(bytes) %}
	{% apply spaceless %}
		{% set kilobyte = 1024 %}
		{% set megabyte = kilobyte * 1024 %}
		{% set gigabyte = megabyte * 1024 %}
		{% set terabyte = gigabyte * 1024 %}

		{% if bytes < kilobyte %}
			{{ bytes ~ ' B' }}
		{% elseif bytes < megabyte %}
			{{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
		{% elseif bytes < gigabyte %}
			{{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
		{% elseif bytes < terabyte %}
			{{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
		{% else %}
			{{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
		{% endif %}
	{% endapply %}
{% endmacro %}

 

 

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...