Merge branch '2.x' into 3.x

* 2.x:
  fixed typo
  fixed tests
  fixed a PHP fatal error when calling a macro imported in the template in a nested block
  fixed typo
  added some tests
  fixed a regression
This commit is contained in:
Fabien Potencier
2019-05-17 08:14:55 +02:00
11 changed files with 181 additions and 3 deletions
+1
View File
@@ -250,6 +250,7 @@
* 1.41.1 (2019-XX-XX)
* fixed a PHP fatal error when calling a macro imported in a block in a nested block
* fixed a PHP fatal error when calling a macro imported in the template in another macro
* fixed wrong error message on "import" and "from"
+13 -1
View File
@@ -266,7 +266,19 @@ class Parser
public function getImportedSymbol(string $type, string $alias)
{
return $this->importedSymbols[0][$type][$alias] ?? null;
if (null !== $this->peekBlockStack()) {
foreach ($this->importedSymbols as $functions) {
if (isset($functions[$type][$alias])) {
if (count($this->blockStack) > 1) {
return null;
}
return $functions[$type][$alias];
}
}
} else {
return $this->importedSymbols[0][$type][$alias] ?? null;
}
}
public function isMainScope(): bool
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{% block foo %}
{%- from _self import input as linput %}
{% endblock %}
{% block bar %}
{{- linput('username') }}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Unknown "linput" function in "index.twig" at line 7.
@@ -1,15 +1,18 @@
--TEST--
"from" tag with syntax error
--TEMPLATE--
{% from _self import another %}
{% from _self import another, foo %}
{{ foo() }}
{% macro foo() %}
{{ another() }}
{% endmacro %}
{% macro another() %}
OK
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Unknown "another" function in "index.twig" at line 5.
Twig\Error\SyntaxError: Unknown "another" function in "index.twig" at line 7.
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{% block foo %}
{%- from _self import input as linput %}
{% block bar %}
{{- linput('username') }}
{% endblock %}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Unknown "linput" function in "index.twig" at line 6.
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{%- from _self import input %}
{% block foo %}
{% block bar %}
{{- input('username') }}
{% endblock %}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\SyntaxError: Unknown "input" function in "index.twig" at line 6.
@@ -0,0 +1,36 @@
--TEST--
"macro" tag
--TEMPLATE--
{% import _self as macros %}
{% from _self import input %}
{% block foo %}
{{- macros.input('username') }}
{{- input('username') }}
{%- import _self as lmacros %}
{%- from _self import input as linput %}
{{- lmacros.input('username') }}
{{- linput('username') }}
{% endblock %}
{% block bar %}
{{- macros.input('username') }}
{{- input('username') }}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXPECT--
<input name="username">
<input name="username">
<input name="username">
<input name="username">
<input name="username">
<input name="username">
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{% block foo %}
{%- import _self as lmacros %}
{% endblock %}
{% block bar %}
{{- lmacros.input('username') }}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Variable "lmacros" does not exist in "index.twig" at line 7.
@@ -0,0 +1,18 @@
--TEST--
"import" tag with syntax error
--TEMPLATE--
{% import _self as foo %}
{{ foo.foo() }}
{% macro foo() %}
{{ foo.another() }}
{% endmacro %}
{% macro another() %}
OK
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Variable "foo" does not exist in "index.twig" at line 7.
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{% block foo %}
{%- import _self as lmacros %}
{% block bar %}
{{- lmacros.input('username') }}
{% endblock %}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Accessing \Twig\Template attributes is forbidden in "index.twig" at line 6.
@@ -0,0 +1,18 @@
--TEST--
"macro" tag
--TEMPLATE--
{%- import _self as macros %}
{% block foo %}
{% block bar %}
{{- macros.input('username') }}
{% endblock %}
{% endblock %}
{% macro input(name) -%}
<input name="{{ name }}">
{% endmacro %}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Accessing \Twig\Template attributes is forbidden in "index.twig" at line 6.