Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to call macro when included instead of imported: Closure call with mismatched arguments #31

Open
Craftplacer opened this issue Oct 16, 2024 · 4 comments

Comments

@Craftplacer
Copy link

Craftplacer commented Oct 16, 2024

I don't know if this is a supported way of using macros, but I thought I should report it here. As a side note: In templates where blocks are used, import statements don't work when not put inside block statements (if the template extends another), leading to me trying this cursed method.

Package version: 0.6.1-dev.5

Error

NoSuchMethodError: Closure call with mismatched arguments: function 'StringSinkRenderer.getMacroFunction.macro'
Receiver: Closure: (List<Object?>, Map<Object?, Object?>) => String
Tried calling: StringSinkRenderer.getMacroFunction.macro(_Map len:18)
Found: StringSinkRenderer.getMacroFunction.macro(List<Object?>, Map<Object?, Object?>) => String
dart:core                                          Function.apply
package:jinja/src/environment.dart 297:21          Environment.callCommon
package:jinja/src/runtime.dart 40:24               Context.call
package:jinja/src/renderer.dart 207:20             StringSinkRenderer.visitCall
package:jinja/src/nodes/expressions.dart 375:20    Call.accept
package:jinja/src/renderer.dart 627:28             StringSinkRenderer.visitInterpolation
package:jinja/src/nodes.dart 76:20                 Interpolation.accept
package:jinja/src/renderer.dart 641:12             StringSinkRenderer.visitOutput
package:jinja/src/nodes.dart 109:20                Output.accept
package:jinja/src/renderer.dart 510:19             StringSinkRenderer.visitFor.render
package:jinja/src/renderer.dart 517:5              StringSinkRenderer.visitFor
[...]

component.jinja

{% macro component(data) %}
{{data}}
{% endmacro %}

page.jinja

{% include 'component.jinja' %}

{{ component("hey") }}
@ykmnkmi
Copy link
Owner

ykmnkmi commented Oct 16, 2024

Can you provide an example with blocks? Use import.

@Craftplacer
Copy link
Author

Craftplacer commented Oct 16, 2024

base.jinja

<html>
<body>
    {% block content %}{% endblock %}
</body>
</html>

page.jinja

{% extends "base.jinja" %}

{% import 'component.jinja' as components %}

{% block content %}
{{ components.component("hey") }}
{% endblock %}

component.jinja

{% macro component(data) %}
{{data}}
{% endmacro %}

@Craftplacer
Copy link
Author

Craftplacer commented Oct 16, 2024

Macro imports in general seem very flaky or not as robust as the examples in the Jinja documentation may lead to believe. For example, importing 2 files under components at the same time would also cause an error:

{% import "components/a.jinja" as components %}
{% import "components/b.jinja" as components %}

{{ components.a() }}
{{ components.b() }}

and

{% from "components/a.jinja" import components as a %}
{% from "components/b.jinja" import components as b %}

{{ a() }}
{{ b() }}
NoSuchMethodError: The getter 'call' was called on null.
Receiver: null
Tried calling: call
dart:core                                          Object.noSuchMethod
package:jinja/src/runtime.dart 37:25               Context.call
package:jinja/src/renderer.dart 207:20             StringSinkRenderer.visitCall
package:jinja/src/nodes/expressions.dart 375:20    Call.accept
package:jinja/src/renderer.dart 627:28             StringSinkRenderer.visitInterpolation
package:jinja/src/nodes.dart 76:20                 Interpolation.accept
package:jinja/src/renderer.dart 641:12             StringSinkRenderer.visitOutput
package:jinja/src/nodes.dart 109:20                Output.accept
package:jinja/src/renderer.dart 510:19             StringSinkRenderer.visitFor.render
package:jinja/src/renderer.dart 517:5              StringSinkRenderer.visitFor
package:jinja/src/nodes/statements.dart 55:20      For.accept
package:jinja/src/renderer.dart 566:17             StringSinkRenderer.visitIf
package:jinja/src/nodes/statements.dart 143:20     If.accept
package:jinja/src/renderer.dart 641:12             StringSinkRenderer.visitOutput
package:jinja/src/nodes.dart 109:20                Output.accept
package:jinja/src/renderer.dart 181:17             StringSinkRenderer.getMacroFunction.macro
dart:core                                          Function.apply
package:jinja/src/environment.dart 297:21          Environment.callCommon
package:jinja/src/runtime.dart 40:24               Context.call
package:jinja/src/renderer.dart 207:20             StringSinkRenderer.visitCall
package:jinja/src/nodes/expressions.dart 375:20    Call.accept
package:jinja/src/renderer.dart 627:28             StringSinkRenderer.visitInterpolation
package:jinja/src/nodes.dart 76:20                 Interpolation.accept
package:jinja/src/renderer.dart 641:12             StringSinkRenderer.visitOutput
package:jinja/src/nodes.dart 109:20                Output.accept
package:jinja/src/renderer.dart 181:17             StringSinkRenderer.getMacroFunction.macro

@ykmnkmi
Copy link
Owner

ykmnkmi commented Oct 22, 2024

The page.jinja template is correct and expected to work. It's a bug, working on it.

{% extends "base.jinja" %}

{% import 'component.jinja' as components %}

{% block content %}
{{ components.component("hey") }}
{% endblock %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants