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 Fletcher07 May 2024
Understanding and resolving a Drupal render array error
Dealing with errors in Drupal development is a common occurrence, and understanding how to interpret and resolve them is essential for smooth development workflows. In this article, we'll delve into a specific error message related to render arrays in Drupal and discuss steps to diagnose and fix the...