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

Include nested template with string #22

Closed
iggyzuk opened this issue Feb 20, 2024 · 2 comments
Closed

Include nested template with string #22

iggyzuk opened this issue Feb 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@iggyzuk
Copy link

iggyzuk commented Feb 20, 2024

Hi, I’m a little puzzled, when including a template can I not pass a simple string inside it using the with keyword?

#[derive(Serialize)]
struct Template {
    colors: Vec<&'static str>,
}

Main template:

{% for color in colors %} 
    {% include "color" with color %} 
{% endfor %}

Color template:

My color is: {{ color }}

The error I get:

  --> color:1:17
   |
 1 | My color is: {{ color }}
   |                 ^^^^^
   |
   = reason: not found in this scope

In order to compile I must do the following but it feels verbose.

#[derive(Serialize)]
struct Template {
    colors: Vec<Color>,
}

#[derive(Serialize, Clone)]
struct Color {
    color: &'static str,
}
@rossmacarthur
Copy link
Owner

Currently with {value} makes {value} the root context for the nested template. So the first error is because it is trying to lookup the field color from the string. The way you are doing it is currently the best way to do, but I agree it is a bit verbose.

You should actually not have not specify with at all, but it appears there is a bug with accessing loop variables in the parent template 😬. I have fixed it in d9e46ea, so the following should work in the next release.

{% for color in colors %} 
    {% include "color" %} 
{% endfor %}

With regards to using with I can see a few potential improvements here:

Improve passing of value into the nested template

  • We could support map literals and allow constructing the literal inline before passing, for example
    {% include "color" with { color: color } %}
    This is still a bit verbose

Improve referring to the root context

  • We could support referring to the current root context with .
    My color is: {{ . }}

@rossmacarthur rossmacarthur added the bug Something isn't working label Feb 23, 2024
@rossmacarthur
Copy link
Owner

Fix is released, I'm going to close this issue, but I have opened #23, and #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants