Macros 检测twig宏参数是否已传递或为null

Macros 检测twig宏参数是否已传递或为null,macros,twig,undefined,Macros,Twig,Undefined,我需要知道twig宏的参数是何时定义的,而null是何时作为值传递的。如果我使用“is defined”,那么这两个条件都适用,因为twig似乎将所有未定义的参数都设置为null 例如,这里有两个调用,第一个调用不带参数的宏,第二个调用带参数null值: {% import 'macros.twig' as macros %} {{ macros.method() }} {{ macros.method(null) }} 这就是宏观定义: {% macro method(value) %}

我需要知道twig宏的参数是何时定义的,而null是何时作为值传递的。如果我使用“is defined”,那么这两个条件都适用,因为twig似乎将所有未定义的参数都设置为null

例如,这里有两个调用,第一个调用不带参数的宏,第二个调用带参数null值:

{% import 'macros.twig' as macros %}
{{ macros.method() }}
{{ macros.method(null) }}
这就是宏观定义:

{% macro method(value) %}
  {# condition to determine if value is undefined or null? #}
{% endmacro %}

为了更深入地了解Twig对宏的定义做了什么,我添加了编译后的源代码。正如您所说,twig将所有变量默认设置为
null
,因此测试变量是否已传递给宏将很困难

小枝

编译源


不解释
null
@DarkBee我想真正的问题是twig将未定义的参数设置为null。感谢分享编译的源代码。
{% macro vars(foo, bar, foobar) %}
{% endmacro %}

{% import _self as forms %}
{{ forms.vars(null, false) }}
// line 1
public function macro_vars($__foo__ = null, $__bar__ = null, $__foobar__ = null, ...$__varargs__)
{
    $context = $this->env->mergeGlobals(array(
        "foo" => $__foo__,
        "bar" => $__bar__,
        "foobar" => $__foobar__,
        "varargs" => $__varargs__,
    ));

    $blocks = array();

    ob_start();
    try {

        return ('' === $tmp = ob_get_contents()) ? '' : new Twig_Markup($tmp, $this->env->getCharset());
    } finally {
        ob_end_clean();
    }
}