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

Allow macros to take self as an argument #880

Merged
merged 2 commits into from
Oct 12, 2023
Merged

Allow macros to take self as an argument #880

merged 2 commits into from
Oct 12, 2023

Conversation

PizzasBear
Copy link
Contributor

Fixes the error no field `self` on type `&Templ` in the following example

#[derive(askama::Template)]
#[template(
    ext = "html",
    source = "
        {% macro m(x) %}
            {{ x.name }}
        {% endmacro %}

        {# this doesn't work #}
        {% call m(self) %}
    "
)]
struct Templ {
    name: String,
}

fn main() {
    println!(
        "{}",
        Templ {
            name: "Templ".into()
        }
    );
}

@PizzasBear
Copy link
Contributor Author

A temporary fix is to wrap self in parentheses, e.g.

#[derive(askama::Template)]
#[template(
    ext = "html",
    source = "
        {% macro m(x) %}
            {{ x.name }}
        {% endmacro %}

        {# this does work #}
        {% call m((self)) %}
    "
)]
struct Templ {
    name: String,
}

fn main() {
    println!(
        "{}",
        Templ {
            name: "Templ".into()
        }
    );
}

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay -- since we know self is a reference, we know we won't be moving a non-Copy value.

Could you maybe add a test for this?

@PizzasBear
Copy link
Contributor Author

I'm pretty new to pull requests and this repo, so I don't know how to add a test...

@djc
Copy link
Owner

djc commented Oct 12, 2023

Basically add your example from the PR description in testing/tests/macro.rs and add a test function to make sure it has the expected output.

Signed-off-by: max <gmx.sht@gmail.com>
@PizzasBear
Copy link
Contributor Author

I've added the test

@djc djc merged commit 5ee2dfb into djc:main Oct 12, 2023
20 checks passed
@djc
Copy link
Owner

djc commented Oct 12, 2023

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants