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

Is it possible to render template into native java types? #956

Closed
Santobert opened this issue Nov 17, 2022 · 2 comments
Closed

Is it possible to render template into native java types? #956

Santobert opened this issue Nov 17, 2022 · 2 comments

Comments

@Santobert
Copy link

With Jinja for python it is possible to use a native environment to render template into native objects.
https://jinja.palletsprojects.com/en/3.1.x/nativetypes/

Is something similar possible with this library?

Jinjava jinjava = new Jinjava();
String template = "{{ value + 2 }}";
var result = jinjava.render(template, Map.of("value", 2));
System.out.println(result); // 4
System.out.println(result.getClass().getName()); // java.lang.Integer

Thank you very much. Feel free to close this issue without comment if such questions are not appreciated.

@jasmith-hs
Copy link
Contributor

You could do something similar using

JinjavaInterpreter interpreter = jinjava.newInterpreter();
interpreter.getContext().put("value", 2);
interpreter.resolveELExpressionSilently("value + 2");

@Santobert
Copy link
Author

Thank you, it works 🎉

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

No branches or pull requests

2 participants