You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry to ask here but I cannot find any example showing how to retrieve all the variables inside a template...
For instance with the following template:
Hello {{ name }}!
{% if score > 80 %}
I'm happy to inform you that you did very well on today's {{ test_name }}.
{% else %}
I'm sorry to inform you that you did not do so well on today's {{ test_name }}.
{% endif %}
You reached {{ score }} out of {{ max_score }} points.
I want to (name, score, test_name, max_score)
Thanks
The text was updated successfully, but these errors were encountered:
@Test
public void jinjaTest() {
String template = "Hello {{ name }}!\n" +
"\n" +
"{% if score > 80 %}\n" +
"I'm happy to inform you that you did very well on today's {{ test_name }}.\n" +
"{% else %}\n" +
"I'm sorry to inform you that you did not do so well on today's {{ test_name }}.\n" +
"{% endif %}\n" +
"You reached {{ score }} out of {{ max_score }} points.";
Jinjava jinjava = new Jinjava();
Node node = jinjava.newInterpreter().parse(template);
List<String> result = new ArrayList<>();
helper(node, result);
System.out.println(result);
}
void helper(Node node, List<String> result) {
if (Objects.isNull(node)) {
return;
}
if (node instanceof ExpressionNode) {
Token token = node.getMaster();
if (token instanceof ExpressionToken) {
result.add(((ExpressionToken) token).getExpr());
}
}
if (!CollectionUtils.isEmpty(node.getChildren())) {
for (Node child: node.getChildren()) {
helper(child, result);
}
}
}
Hi,
Sorry to ask here but I cannot find any example showing how to retrieve all the variables inside a template...
For instance with the following template:
I want to (name, score, test_name, max_score)
Thanks
The text was updated successfully, but these errors were encountered: