Repository: twigphp/Twig (php)

Issue:
Warning in compiled Twig template
Since Twig 3.9.0, we get this warning on some of our templates:
> Warning: Undefined variable $blocks

This is the smallest example I could write:
```twig
{{ _self.mymacro(items, attributes, 0, menu_name) }}

{% macro mymacro(items, attributes, menu_level, menu_name) %}
  {% if items %}
    {% apply spaceless %}
      {% if menu_level == 0 %}
        foo
      {% endif %}
    {% endapply %}
  {% endif %}
{% endmacro %}
```
It seems to come from the code generated here: https://github.com/twigphp/Twig/blob/2105d659574211ce6da200da5867180a1e00c1ef/src/Node/CaptureNode.php#L39

Reverting back to Twig 3.8.0 removes the warning.

Pull request description:
Fix CaptureNode for some use cases
Closes #4022

Patch:
diff --git a/CHANGELOG b/CHANGELOG
index ef5b57fc..07490c53 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,6 @@
 # 3.9.1 (2024-XX-XX)
 
- * n/a
+ * Fix missing `$blocks` variable in `CaptureNode`
 
 # 3.9.0 (2024-04-16)
 
diff --git a/src/Node/MacroNode.php b/src/Node/MacroNode.php
index e44150f5..761ef55d 100644
--- a/src/Node/MacroNode.php
+++ b/src/Node/MacroNode.php
@@ -79,6 +79,9 @@ class MacroNode extends Node
             ;
         }
 
+        $node = new CaptureNode($this->getNode('body'), $this->getNode('body')->lineno, $this->getNode('body')->tag);
+        $node->setAttribute('with_blocks', true);
+
         $compiler
             ->write('')
             ->string(self::VARARGS_NAME)
@@ -88,7 +91,7 @@ class MacroNode extends Node
             ->write("]);\n\n")
             ->write("\$blocks = [];\n\n")
             ->write('return ')
-            ->subcompile(new CaptureNode($this->getNode('body'), $this->getNode('body')->lineno, $this->getNode('body')->tag))
+            ->subcompile($node)
             ->raw("\n")
             ->outdent()
             ->write("}\n\n")


Test patch:
diff --git a/tests/Fixtures/macros/macro_with_capture.test b/tests/Fixtures/macros/macro_with_capture.test
new file mode 100644
index 00000000..f67a7fcd
--- /dev/null
+++ b/tests/Fixtures/macros/macro_with_capture.test
@@ -0,0 +1,14 @@
+--TEST--
+macro
+--TEMPLATE--
+{{ _self.some_macro() }}
+
+{% macro some_macro() %}
+    {% apply spaceless %}
+        {% if true %}foo{% endif %}
+    {% endapply %}
+{% endmacro %}
+--DATA--
+return []
+--EXPECT--
+foo