mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.67k stars 101 forks source link

Add support for the `*` operator #518

Closed GabrielBianconi closed 5 months ago

GabrielBianconi commented 5 months ago

Jinja2 supports e.g. {{ ' ' * 10 }} but that doesn't work with MiniJinja. We were able to circumvent the issue with a recursive macro, but that operator would simplify things!

Error: invalid operation: tried to use * operator on unsupported types string and number (in template.html:15)

Below is the workaround in case anyone else is having the same issue.

{%- macro repeat(s, times) -%}
  {%- if times >= 1 -%}
    {{ s }}

    {%- if times > 1 -%}
      {{ repeat(s, times - 1) }}
    {%- endif -%}

  {%- endif -%}
{%- endmacro -%}

{{ repeat("abc", 4) }}
mitsuhiko commented 5 months ago

That makes sense. Would also need to support sequences and potentially iterators. I will look into it.

GabrielBianconi commented 5 months ago

Thanks!