Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.54k stars 283 forks source link

Combined Usage of Macro and a variable to dynamically Include a Partial / Template #749

Closed DK26 closed 2 years ago

DK26 commented 2 years ago

I have read in the documentation that it is possible to somehow dynamically assign an include to a template: Tera doesn't offer passing a custom context to the include tag. If you want to do that, use macros.

But as much as I digged the docs, I did not find any example or a proper combination to make that work.

I encountered issue #603 but that wasn't helpful neither. Putting a macro with the next value:

{% macro type(id) %}
{% include "{{ id }}.html" %}
{% endmacro input %}

Would simply produce an error: Template '[{{ id }}.html]' not found.

Is there a way to achieve this?

Keats commented 2 years ago

I have read in the documentation that it is possible to somehow dynamically assign an include to a template

You can't include a template with a dynamic name, it has to be a string not an ident.

DK26 commented 2 years ago

So just to make sure I understand correctly: It is NOT possible to use a macro to achieve this and the comment in the documentation is out-dated and therefore misleading?

Or, the comment in the docs meant to say something else?

Keats commented 2 years ago

The latter. Context here means variables. You can't do include "blabla.html" with foo = 2 but you can define a macro that takes a foo argument.

DK26 commented 2 years ago

The latter. Context here means variables. You can't do include "blabla.html" with foo = 2 but you can define a macro that takes a foo argument.

Ohhh I see now 🤦‍♂️. It's about passing down context / variables to an external template.. lol😅

Thank you for your time and patience!