Skip to content

Commit

Permalink
Include: jekyll: include variables are mapped to "include" scope
Browse files Browse the repository at this point in the history
In the included file, variables passed as arguments to the include tag
are accessible via the "include" scope, not directly (e.g., include.var
instead of var)

Fixes incomplete implementation in a48b5f8.

Fixes #194
  • Loading branch information
kohlschuetter committed Nov 30, 2022
1 parent dffbe6b commit fc25966
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions _includes/include_read_include_var.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ include.var }}
14 changes: 8 additions & 6 deletions src/main/java/liqp/tags/Include.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ public Object render(TemplateContext context, LNode... nodes) {
template = context.getParser().parse(includeResourceFile);
}

Map<String, Object> variables = new HashMap<String, Object>();

if (nodes.length > 1) {
if (context.parseSettings.flavor != Flavor.JEKYLL) {
// check if there's a optional "with expression"
Object value = nodes[1].render(context);
context.put(includeResource, value);
} else {
} else {
// Jekyll-style variable assignments
Map<String, Object> variables = new HashMap<String, Object>();
Map<String, Object> includeMap = new HashMap<>();
variables.put("include", includeMap);
for (int i = 1, n = nodes.length; i < n; i++) {
@SuppressWarnings("unchecked")
Map<String, Object> var = (Map<String, Object>) nodes[i].render(context);
variables.putAll(var);
includeMap.putAll(var);
}
return template.renderUnguarded(variables, context, true);
}
}
}
return template.renderUnguarded(context);

return template.renderUnguarded(variables, context, true);
} catch (Exception e) {
if (context.renderSettings.showExceptionsFromInclude) {
throw new RuntimeException("problem with evaluating include", e);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public void testIncludeVariableSyntaxTagDefaultJekyll() {

@Test
public void testIncludeWithExpression() {
Template template = Template.parse("{% include include_read_var var=otherVar %}", jekyll());
Template template = Template.parse("{% include include_read_include_var var=otherVar %}", jekyll());
String res = template.render("{ \"otherVar\" : \"TEST\"}");
assertEquals("TEST", res);
}

@Test
public void testIncludeWithMultipleExpressions() {
Template template = Template.parse(
"{% include include_read_var foo=bar var=otherVar var=\"var\" var=yetAnotherVar %}", jekyll());
"{% include include_read_include_var foo=bar var=otherVar var=\"var\" var=yetAnotherVar %}", jekyll());
String res = template.render("{ \"otherVar\" : \"TEST\", \"yetAnotherVar\": \"ANOTHER\"}");
assertEquals("ANOTHER", res);
}
Expand Down

0 comments on commit fc25966

Please sign in to comment.