Skip to content

Deprecate the spaceless filter#4236

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:spaceless-filter-deprecation
Aug 26, 2024
Merged

Deprecate the spaceless filter#4236
fabpot merged 1 commit into
twigphp:3.xfrom
fabpot:spaceless-filter-deprecation

Conversation

@fabpot
Copy link
Copy Markdown
Contributor

@fabpot fabpot commented Aug 26, 2024

Closes #3576

Deprecate the spaceless filter for the following reasons:

If someone find it useful, re-creating it is trivial (return trim(preg_replace('/>\s+</', '><', $content ?? ''));),
but with so many caveats and not so many use cases, I think it does not belong to core.

Copy link
Copy Markdown
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filter should be marked as deprecated in the filter definition, so that using the filter reports the deprecation at compile time (and so that the linter also reports it)

Comment thread src/Extension/CoreExtension.php Outdated
@fabpot fabpot force-pushed the spaceless-filter-deprecation branch from 0660869 to 6f4be01 Compare August 26, 2024 14:24
@fabpot fabpot force-pushed the spaceless-filter-deprecation branch from 6f4be01 to 9fd4c48 Compare August 26, 2024 14:25
@ttk
Copy link
Copy Markdown

ttk commented Mar 3, 2025

I didn't find any good examples of how to replace spaceless with the existing whitespace controls. So here's what I did:

{% apply spaceless %}
  <span>1</span>
  <span>2</span>
  <span>3</span>
{% endapply %}

To rewrite this without spaceless, I used {{ }} with a null condition and the appropriate whitespace controls:

  <span>1</span>{{ null -}}
  <span>2</span>{{ null -}}
  <span>3</span>

I experimented with other "null" conditions, and the following also work: {{ '' -}} , {{ "" -}} , and {{ false -}}, but I find {{ null -}} to be my personal preference.

Not sure if there is a better syntax to handle this? It might be worth adding this example to the docs?

@stof
Copy link
Copy Markdown
Member

stof commented Mar 4, 2025

@ttk the shortest solution (and most efficient by disappearing entirely at runtime) is to use a comment to hold the whitespace control: {# -#}

@ttk
Copy link
Copy Markdown

ttk commented Mar 4, 2025

@stof Thanks, that is exactly what I was looking for! I honestly didn't know that whitespace control worked on comments as well but after re-reviewing the docs, I see a single example shown here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Extension::spaceless: Algorithm removes spaces in the text and causes the meaning to be broken.

3 participants