djc / askama

Type-safe, compiled Jinja-like templates for Rust
Apache License 2.0
3.35k stars 215 forks source link

Allow support for control blocks in (named) arguments? #987

Open GuillaumeGomez opened 5 months ago

GuillaumeGomez commented 5 months ago

Currently, we can't pass expressions as arguments to macros (or filters). So code like this doesn't work:

    {%
        call release_macros::header(
            {% if description.is_empty() %}""{% else %}description{% endif %},
            tab=release_type,
            owner={% if let Some(owner) = owner %}owner{% else %}false{% endif %}
        )
    %}

Would you agree to add support for this?

djc commented 5 months ago

Ugh. Are there workarounds, and how bad are they? Is there precedent for this in other Jinja(-like) engines?

GuillaumeGomez commented 5 months ago

In tera at least, can't find anything mentioned about it in the official jinja package in python though.

And the workaround isn't that ugly, you just declare a variable above instead:

    {% set owner = false %}
    {% if let Some(owner_val) = owner %}
        {% set owner = owner_val %}
    {% endif %}
    {%
        call release_macros::header(
            {% if description.is_empty() %}""{% else %}description{% endif %},
            tab=release_type,
            owner=owner
        )
    %}
djc commented 5 months ago

Okay, let's keep this open and see if anyone else turns up with similar requirements.