Commit Graph

8227 Commits

Author SHA1 Message Date
Nicolas Grekas 10c9e9946b Merge branch '3.x' into 4.x
* 3.x:
  Fix the empty comment "{##}" being lexed as a documentation comment opening
2026-08-20 20:40:40 +02:00
Fabien Potencier 84c5051151 bug #4891 Fix the empty comment "{##}" being lexed as a documentation comment opening (Amoifr)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Fix the empty comment "{##}" being lexed as a documentation comment opening

Since #4871, the empty comment `{##}` fails to lex: the source is `{#` immediately followed by the closing `#}`, but the lexer now reads it as the documentation comment opening `{##`, which steals the `#` belonging to the closing tag. The remaining input never contains a closer, so lexing fails with `Unclosed comment`, and in a larger template everything up to the next comment gets swallowed, which surfaces as confusing errors like `Expected endblock for block "form_row_render" (but "submit_row" given)`.

Minimal reproducer:

```php
(new \Twig\Environment(new \Twig\Loader\ArrayLoader()))->createTemplate('{##}')->render();
// Twig\Error\SyntaxError: Unclosed comment at line 1.
```

This is currently breaking the symfony/symfony 8.2 CI on every new run: the Twig bridge form themes use `{##}` as a line-joining trick (e.g. `bootstrap_3_horizontal_layout.html.twig`), so all form layout tests fail to compile ([example run](https://github.com/symfony/symfony/actions/runs/32267973483/job/96117211040), 547 errors).

The fix adds a negative lookahead to the documentation comment opening so that the exact sequence `{##}` keeps lexing as a regular (empty) comment. The lookahead is only built when the comment closing tag starts with `#`, so custom delimiters where the ambiguity cannot exist (and where an empty documentation comment would legitimately match the lookahead) are unaffected. `{###}` (an empty documentation comment) and `{##-#}` keep working.

Commits
-------

c9c3b23a77 Fix the empty comment "{##}" being lexed as a documentation comment opening
2026-08-19 19:01:15 +02:00
Pascal CESCON - Amoifr c9c3b23a77 Fix the empty comment "{##}" being lexed as a documentation comment opening 2026-08-19 19:01:11 +02:00
Fabien Potencier 8021c1a05c Fix merge conflict resolution 2026-08-19 16:46:10 +02:00
Fabien Potencier d7fc3d6dc9 Merge branch '3.x' into 4.x
* 3.x:
  Attach documentation comments to nodes

# Conflicts:
#	CHANGELOG
#	src/Lexer.php
#	src/Node/Node.php
#	src/Parser.php
#	src/Token.php
2026-08-19 16:44:35 +02:00
Fabien Potencier 36b8920932 feature #4871 Attach documentation comments to nodes (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Attach documentation comments to nodes

Alternatives to #4870

To avoid BC breaks, I have another idea, using `##` as a new syntax, a bit like `/** */` in PHP vs `/* */`.

"Documentation" is attached as metadata to the next relevant node:

```twig
{## The main content displayed on the page #}
{% block content %}
    ...
{% endblock %}
```

Documentation comments can also describe variables declared with the `types` tag:

```twig
{% types {
    ## The unique identifier of the article
    id: 'string',

    ## Whether the article should be highlighted
    featured?: 'boolean',
} %}
```

Node visitors can access this metadata through `Node::getDocumentation()`, allowing IDEs, static analyzers, and documentation generators to consume it without affecting template rendering. Documentation is preserved when visitors or optimizations replace nodes.

Closes #4768
Closes #4870

Commits
-------

6806e30474 Attach documentation comments to nodes
2026-08-19 16:41:26 +02:00
Fabien Potencier 6806e30474 Attach documentation comments to nodes 2026-08-19 16:41:17 +02:00
Fabien Potencier 553980ccff Use PHPUnit attribute for Markdown data provider 2026-08-17 20:22:03 +02:00
Fabien Potencier c4d77c9847 Remove versionadded in docs 2026-08-17 20:19:13 +02:00
Fabien Potencier 60d3b3452a Merge branch '3.x' into 4.x
* 3.x:
  Add support for tempest/markdown in markdown-extra
  Add the include_only function to render a template without access to the current context
  Clarify duplicate macro deprecation message
  Deduplicate template error handling

# Conflicts:
#	.gitignore
#	CHANGELOG
#	extra/markdown-extra/composer.json
#	src/Extension/CoreExtension.php
#	src/Parser.php
#	src/Template.php
#	tests/Fixtures/macros/duplicate_definition.legacy.test
2026-08-16 10:57:50 +02:00
Fabien Potencier 9036d8c5aa feature #4434 Add the include_only function (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Add the include_only function

Commits
-------

dc8f96df3e Add the include_only function to render a template without access to the current context
2026-08-16 10:53:58 +02:00
Fabien Potencier 2bb20e283f feature #4890 Add support for tempest/markdown in markdown-extra (ker0x)
This PR was merged into the 3.x branch.

Discussion
----------

Add support for tempest/markdown in markdown-extra

Adds `TempestMarkdown`, an adapter for [`tempest/markdown`](https://github.com/tempestphp/markdown), alongside the existing `LeagueMarkdown`, `MichelfMarkdown` and `ErusevMarkdown` implementations. It follows the same pattern as the others and accepts a pre-configured `Tempest\Markdown\Markdown` in its constructor, so rules and the highlighter can be customized.

It is also appended as the **last** branch of `DefaultMarkdown`'s discovery chain, so projects that already have another library installed keep resolving to it exactly as before.

### PHP requirement

Every published version of `tempest/markdown` requires PHP `^8.5`, while `twig/markdown-extra` supports `>=8.1`. So:

- it is declared in `require-dev` only;
- a CI step removes it before `composer install` on PHP < 8.5, mirroring the existing conditional step used for `twig-extra-bundle`;
- `FunctionalTest` only adds it to the converter matrix when `Tempest\Markdown\Markdown` exists.

The suite passes both with and without the library installed.

### Test data change

Three cases in `getMarkdownTests()` used Setext headings (`Hello` underlined with `=====`). `tempest/markdown` only implements ATX headings, so those were switched to `# Hello`. Those cases exercise the filter plumbing (`{% apply %}`, indentation stripping, `include()|markdown_to_html`) rather than the Markdown dialect, so no coverage is lost.

Two patterns were also relaxed for the same reason: `<h1[^>]*>` because Tempest emits auto heading ids, and `<p>…\s*</p>` because it keeps the source's trailing newline inside the final paragraph. Both remain accurate for the other converters.

These differences, plus the fact that front matter is parsed out rather than rendered, are documented in a note in `doc/filters/markdown_to_html.rst`.

### Unrelated one-liner

The last commit also adds `.php-cs-fixer.cache` to `.gitignore` — it is generated by the project's own `php-cs-fixer` dev dependency and was showing up as untracked. Happy to split it out if you'd rather keep this PR to a single concern.

Commits
-------

aa17f59877 Add support for tempest/markdown in markdown-extra
2026-08-13 10:02:40 +02:00
Romain Monteil aa17f59877 Add support for tempest/markdown in markdown-extra 2026-08-12 13:02:21 +02:00
Fabien Potencier dc8f96df3e Add the include_only function to render a template without access to the current context 2026-08-11 20:58:31 +02:00
Fabien Potencier 6fafa104c4 tidy #4883 Clarify duplicate macro deprecation message (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Clarify duplicate macro deprecation message

Commits
-------

8ca4866a95 Clarify duplicate macro deprecation message
2026-08-03 21:22:23 +02:00
Fabien Potencier e1bc2814d5 tidy #4882 Deduplicate template error handling (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Deduplicate template error handling

Commits
-------

e62abbf528 Deduplicate template error handling
2026-08-03 18:11:07 +02:00
Fabien Potencier 8ca4866a95 Clarify duplicate macro deprecation message 2026-08-03 17:35:03 +02:00
Fabien Potencier e62abbf528 Deduplicate template error handling 2026-08-03 17:28:46 +02:00
Fabien Potencier 93b00ee3c9 Merge branch '3.x' into 4.x
* 3.x:
  Nested macro imports

# Conflicts:
#	CHANGELOG
#	src/MacroNamespace.php
#	src/Node/MacrosNode.php
#	src/Parser.php
#	tests/Extension/SandboxStateChangeTest.php
#	tests/Node/MacrosTest.php
2026-08-03 10:34:02 +02:00
Fabien Potencier 2a1df4e64f feature #4881 Nested macro imports (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Nested macro imports

Closes #4879
Replaces #4880

Commits
-------

b53e100444 Nested macro imports
2026-08-03 10:30:38 +02:00
Fabien Potencier b53e100444 Nested macro imports 2026-08-03 10:30:32 +02:00
Fabien Potencier d5e2f249d2 Remove deprecated macro defined test syntax 2026-07-31 12:18:02 +02:00
Fabien Potencier ab8e41b49b Merge branch '3.x' into 4.x
* 3.x:
  Deprecate using parentheses when testing a macro with the defined test

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
#	src/ExpressionParser/Infix/DotExpressionParser.php
#	src/ExpressionParser/Infix/FunctionExpressionParser.php
#	src/ExpressionParser/Infix/IsExpressionParser.php
#	tests/Fixtures/macros/call_without_parentheses.legacy.test
#	tests/ParserTest.php
2026-07-31 12:15:12 +02:00
Fabien Potencier 7793b6153f feature #4878 Deprecate using parentheses when testing a macro with the defined test (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Deprecate using parentheses when testing a macro with the defined test

Commits
-------

34d9c67d38 Deprecate using parentheses when testing a macro with the defined test
2026-07-31 12:09:26 +02:00
Fabien Potencier 34d9c67d38 Deprecate using parentheses when testing a macro with the defined test 2026-07-31 12:09:23 +02:00
Fabien Potencier ce013c063e Remove deprecated code 2026-07-30 14:14:10 +02:00
Fabien Potencier efd847f456 Fix merge conflict resolution 2026-07-30 14:04:07 +02:00
Fabien Potencier 986a765019 Merge branch '3.x' into 4.x
* 3.x:
  Redesign macro calls and argument handling

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
#	src/ExpressionParser/Infix/ArgumentsTrait.php
#	src/ExpressionParser/Infix/DotExpressionParser.php
#	src/ExpressionParser/Infix/FunctionExpressionParser.php
#	src/Extension/CoreExtension.php
#	src/Node/Expression/MacroReferenceExpression.php
#	src/Node/Expression/MethodCallExpression.php
#	src/Node/Expression/TempNameExpression.php
#	src/Node/MacroNode.php
#	src/Node/ModuleNode.php
#	src/Template.php
#	tests/Fixtures/macros/call_without_parentheses.legacy.test
#	tests/Node/Expression/MacroReferenceTest.php
#	tests/Node/MacroTest.php
2026-07-30 14:02:30 +02:00
Fabien Potencier 6dfc7961a2 feature #4851 Redesign macro calls and argument handling (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Redesign macro calls and argument handling

This is my attempt to make macros "better". It uses modern PHP features that didn't exist when I designed macros a long time ago.

The first objective is to close the gap between their behavior and the behavior of Twig callables: functions, filters, and tests.

Here are some important changes:

* Calling a macro without passing a value for an argument that has no default value is deprecated; it is currently silently passed as `null`.
* Passing extra positional arguments or unknown named arguments to a macro without an explicit variadic argument is deprecated; these arguments are currently silently accepted through the implicit `varargs` variable.
* Explicit variadic macro arguments are now supported with `...name`.
* Macros are compiled as closures stored in the macro registry, instead of public generated `macro_*` methods.

The refactor introduces `TwigMacro` and `MacroArgument` to represent template-defined macros with an explicit signature, similar to the existing Twig callable model.

Commits
-------

d7f8b4eb1c Redesign macro calls and argument handling
2026-07-30 13:53:27 +02:00
Fabien Potencier d7f8b4eb1c Redesign macro calls and argument handling 2026-07-30 13:53:24 +02:00
Fabien Potencier b34f022583 Remove deprecated code 2026-07-30 13:52:37 +02:00
Fabien Potencier feffcb7df8 Fix merge conflict resolution 2026-07-30 13:32:38 +02:00
Fabien Potencier 3197df7b5e Merge branch '3.x' into 4.x
* 3.x:
  Make the sandbox a first-class citizen with a dedicated Sandbox class

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
#	doc/tags/sandbox.rst
#	phpstan-baseline.neon
#	src/Extension/CoreExtension.php
#	src/Extension/SandboxExtension.php
#	src/Sandbox/SecurityPolicy.php
2026-07-30 13:04:50 +02:00
Fabien Potencier e315d6d396 feature #4854 Make the sandbox a first-class citizen with a dedicated Sandbox class (fabpot)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Make the sandbox a first-class citizen with a dedicated Sandbox class

I've been thinking about making the sabdbox feature as a first class citizen for years. With all the work that has been done recently on security issues, I spent some time on it again. Here is the result.

The main ideas:

* Currently, the sandbox is thigtly coupled to the "main" environment: `SandboxExtension` is registered on the environmen directly, so it instruments all compiled template, and adds runtime checks to all renders, trusted or not. As recommended in the docs, you should have a dedicated environment for sandboxes, different from the main one, but it's not really "enforced" nor natural to do.
* As a consequence, we store some state via `enableSandbox()`/`disableSandbox()` with try/finally patterns scattered across the codebase to support rendering sandboxed and non-sandboxed templates from a environment.
* When using one environment, a sandboxed template can `include` anything the loader can load, sees every application global, and inherits all extensions, this is a footgun (again, already not recommended in the docs).
* There are too maybe "knobs": global mode, `enableSandbox()`, `{% include(..., sandboxed: true) %}`, and `{% sandbox %}`.

The new `Twig\Sandbox\Sandbox` class renders untrusted templates through a dedicated, always-sandboxed environment crafted by the developer. Taht way, there is no state to toggle and nothing leaks between the main environment and the sandbox, in either direction.

Commits
-------

b762bc94b9 Make the sandbox a first-class citizen with a dedicated Sandbox class
2026-07-30 12:05:54 +02:00
Fabien Potencier b762bc94b9 Make the sandbox a first-class citizen with a dedicated Sandbox class 2026-07-30 12:05:47 +02:00
Fabien Potencier 096da67e7b Remove deprecated macro calls without parentheses 2026-07-28 16:39:05 +02:00
Fabien Potencier 9b42c9bd3d Merge branch '3.x' into 4.x
* 3.x:
  Deprecate macro calls without parentheses

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
2026-07-28 16:26:41 +02:00
Fabien Potencier 5bbbe0d8d6 feature #4877 Deprecate macro calls without parentheses (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Deprecate macro calls without parentheses

Commits
-------

ad305b414e Deprecate macro calls without parentheses
2026-07-28 16:24:59 +02:00
Fabien Potencier ad305b414e Deprecate macro calls without parentheses 2026-07-28 16:13:23 +02:00
Fabien Potencier 8bfb29b953 Remove deprecated code 2026-07-28 14:52:35 +02:00
Fabien Potencier 80df3b5f63 Merge branch '3.x' into 4.x
* 3.x:
  Rename macro variable AST nodes

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
#	src/ExpressionParser/Infix/DotExpressionParser.php
#	src/Node/Expression/Variable/TemplateVariable.php
#	src/Node/ImportNode.php
#	src/Parser.php
2026-07-28 14:47:56 +02:00
Fabien Potencier a2a4c1b4f2 feature #4876 Rename macro variable AST nodes (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Rename macro variable AST nodes

The current name are just wrong as these classes are only used in the context of macros. They were confusing.

Commits
-------

be36fee09e Rename macro variable AST nodes
2026-07-28 12:07:42 +02:00
Fabien Potencier be36fee09e Rename macro variable AST nodes 2026-07-28 11:53:50 +02:00
Fabien Potencier 2100129669 Remove deprecated code 2026-07-28 10:13:16 +02:00
Fabien Potencier d266b2df7f Merge branch '3.x' into 4.x
* 3.x:
  Clarify the security scope for untrusted templates
  Normalize destructuring assignment targets
  Deprecate duplicate macro definitions

# Conflicts:
#	CHANGELOG
#	doc/deprecated.rst
#	tests/ExpressionParserTest.php
2026-07-28 09:58:56 +02:00
Fabien Potencier ccc4a80ba7 documentation #4875 Clarify the security scope for untrusted templates (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Clarify the security scope for untrusted templates

Commits
-------

222a7f3f9a Clarify the security scope for untrusted templates
2026-07-28 09:51:25 +02:00
Fabien Potencier 222a7f3f9a Clarify the security scope for untrusted templates 2026-07-28 08:40:21 +02:00
Fabien Potencier 239cf25362 feature #4874 Normalize destructuring assignment targets (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Normalize destructuring assignment targets

Commits
-------

6ce5beb7c2 Normalize destructuring assignment targets
2026-07-27 18:57:15 +02:00
Fabien Potencier 6ce5beb7c2 Normalize destructuring assignment targets 2026-07-27 18:51:15 +02:00
Fabien Potencier 44f6f7e6c8 feature #4873 Deprecate duplicate macro definitions (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Deprecate duplicate macro definitions

Commits
-------

258089b8a6 Deprecate duplicate macro definitions
2026-07-27 12:45:09 +02:00