public function testMacrosInASandbox()
{
$twig = $this->getEnvironment(true, array('autoescape' => true), array('index' => <<<EOF
-{% macro test(text) %}<p>{{ text }}</p>{% endmacro %}
-{{ _self.test('username') }}
+{%- import _self as macros %}
+
+{%- macro test(text) %}<p>{{ text }}</p>{% endmacro %}
+
+{{- macros.test('username') }}
EOF
- ), array('macro'), array('escape'));
+ ), array('macro', 'import'), array('escape'));
$this->assertEquals('<p>username</p>', $twig->loadTemplate('index')->render(array()));
}
--TEST--
Twig parses postfix expressions
--TEMPLATE--
+{% import _self as macros %}
{% macro foo() %}foo{% endmacro %}
{{ 'a'|upper }}
{{ ('a')|upper }}
{{ -1|upper }}
-{{ _self.foo() }}
-{{ (_self).foo() }}
+{{ macros.foo() }}
+{{ (macros).foo() }}
--DATA--
return array();
--EXPECT--
--TEST--
"macro" tag
--TEMPLATE--
-{{ _self.input('username') }}
-{{ _self.input('password', null, 'password', 1) }}
+{% import _self as macros %}
+
+{{ macros.input('username') }}
+{{ macros.input('password', null, 'password', 1) }}
{% macro input(name, value, type, size) %}
<input type="{{ type|default("text") }}" name="{{ name }}" value="{{ value|e|default('') }}" size="{{ size|default(20) }}">
--TEST--
"macro" tag supports name for endmacro
--TEMPLATE--
-{{ _self.foo() }}
-{{ _self.bar() }}
+{% import _self as macros %}
+
+{{ macros.foo() }}
+{{ macros.bar() }}
{% macro foo() %}foo{% endmacro %}
{% macro bar() %}bar{% endmacro bar %}
--TEST--
"☃" as a macro name
--TEMPLATE--
-{{ _self.☃('foo') }}
+{% import _self as macros %}
+
+{{ macros.☃('foo') }}
{% macro ☃(foo) %}
☃{{ foo }}☃