Skip to content

[3.x] macros not working with null coalescing operator #4776

Description

@anita-chouhan

Description

Twig macro variables don't work when imported from a separate file, and a fallback is provided using a null coalescing operator. However, macros defined within the same template are callable.

Steps to reproduce

  1. Create a macro in a helper.twig file.
{% macro foo(param) %}
  {{ param }}
{% endmacro %}
  1. Call this macro in a different file.
{% from 'helper.twig' import foo %}
{{ foo('bar') ?? '' }}

Expected behaviour

Output string.

Actual behaviour

Error: Call to a member function hasMacro() on null.

When called within the same file, it works fine

{% macro foo(param) %}
  {{ param }}
{% endmacro %}

{{ _self.foo('bar') ?? '' }}
{# Output: bar #}

PHP version

8.2.20

Twig version

3.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions