pebble icon indicating copy to clipboard operation
pebble copied to clipboard

In-memory templates

Open rendaw opened this issue 5 years ago • 3 comments

#228 didn't really get an answer, and I think may also be outdated now. Is there a way to load templates from memory? Not all production uses require inheritance and other features, and writing a template to disk just to load it is a hack at best.

I think getLiteralTemplate(String templateName) might be it but the parameter is templateName with no way to pass in the template body, and the only addition to the description from getTemplate is using a StringLoader which I couldn't make heads or tails of.

rendaw avatar Sep 28 '20 14:09 rendaw

getLiteralTemplate is indeed what you are after. It uses whatever string you pass to it as the template

nicky9door avatar Oct 22 '20 20:10 nicky9door

Okay, thanks! Perhaps the documentation could be expanded to make this clear.

rendaw avatar Oct 31 '20 05:10 rendaw

StringLoader is heavily used in unit tests, for example: https://github.com/PebbleTemplates/pebble/blob/2634c1c7c1664e1b4d946bcc463cd1ca84d5f252/pebble/src/test/java/com/mitchellbosecke/pebble/TernaryExpressionTest.java#L32-L37

If you configure your PebbleEngine to use a StringLoader, then you simply need to call pebble.getTemplate("Hello, {{who | capitalize}}").

bjansen avatar Dec 18 '20 14:12 bjansen