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

retain scope when including a template #118

Merged
merged 2 commits into from
Sep 14, 2019
Merged

retain scope when including a template #118

merged 2 commits into from
Sep 14, 2019

Conversation

thomastrapp
Copy link
Contributor

Example:

nlohmann::json data;
data["cities"] = nlohmann::json::array({{{"name", "Munich"}}, {{"name", "New York"}}});
data["locations"] = nlohmann::json::array({{{"name", "Amsterdam"}}, {{"name", "Mumbai"}}});

inja::Environment env;
env.include_template("item.tpl", env.parse("{{ loop.index1 }}: {{ item.name }}\n"));

env.render_to(
    std::cout,
    env.parse("{% for item in cities %}{% include \"item.tpl\" %}{% endfor %}"),
    data);
env.render_to(
    std::cout,
    env.parse("{% for item in locations %}{% include \"item.tpl\" %}{% endfor %}"),
    data);

// Output:
// 1: Munich
// 2: New York
// 1: Amsterdam
// 2: Mumbai

Again, if there is anything that I can do to get this merged, please let me know.
Thanks!

* Use data from current scope when rendering an included template.
  This allows included templates to access loop variables, which
  was not possible before.
* Add test
@pantor pantor merged commit 057075b into pantor:master Sep 14, 2019
@pantor
Copy link
Owner

pantor commented Sep 14, 2019

LGTM!

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 this pull request may close these issues.

2 participants