Skip to content

Macros include files from the macro's declaration directory, rather than caller's #551

@m4tx

Description

@m4tx

Describe the bug
Both Jinja2 and Askama allow users to define macros. They allow reusing code, which is especially useful when the macro is defined in a separate file and imported in templates.

However, it seems like there is a slight difference in how the macros are processed between these two libraries. When my macro contains a {% include "somefile.html" %} tag, in case of Jinja2, the file is included from the caller's directory. In the case of Askama however, the file is included from the macro's declaration directory, somewhat limiting its usefulness. This also makes them pretty much identical to just using {% include %}, with the only difference being that one can have many different macros in a single file.

The use case for this is that I have a macro that includes a predefined file containing some instructions for the current page and wraps it inside some HTML for styling. I use the macro in many different templates in separate directories, where each one has a different file meant to be included.

To Reproduce
This needs the following files:

.
├── inner
│   └── macro.html
├── template.html
└── to_include.html

template.html

{% import "inner/macro.html" as macros %}

{{ macros.test() }}                                                                                                                                                              

(this is for Jinja2 - for Askama, macros.test() becomes macros::test())

to_include.html

test

inner/macro.html

{% macro test() %}
    {% include "to_include.html" %}
{% endmacro %}

In Jinja2, when "template.html" is rendered, it returns test. In Askama, it throws an error template "to_include.html" not found in directories ....

Askama version
0.14, same issue on latest master (d670d9b)

Rust version
1.90 (nightly), same on 1.88 stable

Metadata

Metadata

Assignees

No one assigned

    Labels

    deriveRelated to the code generatingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions