twigphp / twigphp/Twig

New "spaceless" alternative mode where whitespace is opt-in with `{#+`.

Open
#4,723 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
8.4k
Forks
1.3k
Avg merge
1d 13h
Merged PRs (30d)
37

Description

Current situation

Currently, adding a - to a twig tag causes whitespace removal.
E.g. {#- some comment #}, here whitespace is removed before but not after the comment.

Without the -, a single whitespace between twig tags is removed, but a double line break \n\n is not.

Proposal

I would like to propose a new mechanism that turns this around.

We introduce a new mode-switching twig tag {% spaceremoved %}, which would typically be placed at the top of a template.
(I would call it "spaceless" but that term now has a history. Open to suggestions.)

This allows to opt-in to this mechanism per template or per snippet, thus avoiding BC problems.

When this mode is enabled:

  • A twig tag without - is treated as if it had -. E.g. {# some comment #} is treated like {#- some comment #}.
  • A twig tag with + is treated like a regular tag without -. So, {#+ some comment +#} is treated like {# some comment #}.
Example
{% spaceremoved %} 
hel {# remove space before and after #} lo {#+ keep space before #} world

This is equivalent to

hel {#- remove space before and after -#} lo {# keep space before -#} world

which prints as

hello world
See also

I found

  • #4442, which discusses the same problem, but does not propose this specific solution.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named in the issue. Start by reviewing the existing whitespace-control behavior and the related discussion in #4442, then clarify the syntax and backward-compatibility requirements for the proposed mode. Done means the behavior and implementation scope are agreed and covered by suitable tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.