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

Add support for the * operator #518

Closed
GabrielBianconi opened this issue Jun 6, 2024 · 2 comments · Fixed by #519
Closed

Add support for the * operator #518

GabrielBianconi opened this issue Jun 6, 2024 · 2 comments · Fixed by #519

Comments

@GabrielBianconi
Copy link

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
Copy link
Owner

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

@GabrielBianconi
Copy link
Author

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 a pull request may close this issue.

2 participants