pebble
pebble copied to clipboard
take Paths and Files
would be useful for static typing and debugging purposes to have some of the methods take Path and File
for example I'm currently doing this
private Path dir;
loader.setPrefix(dir.toString());
var template = engine.getTemplate(skel.getSourcePath().toString());
this isn't particularly useful
com.mitchellbosecke.pebble.error.LoaderException: Could not find template "TestApplication.java.pebble" (?:?)
but if I change my code to
var template = engine.getTemplate(skel.getSourcePath().toAbsolutePath().toString());
suddenly my error message contains the full path it was looking for, and I can see exactly why it couldn't find it. pebble could use Path.resolve to do this internally. It would make my code simpler and easier to debug. My code could become
private Path dir;
loader.setPrefix(dir);
var template = engine.getTemplate(skel.getSourcePath());
additionally you could use Files.list() which returns a Stream<Path> easier to iterate over templates in a single directory.
lastly, IMHO, strings are the weakest static type, because people (have to) put lots of naughty things in them, nothing really guarantees that the string that you pass is what you mean to these methods is what you mean.
note: I mention File because it seem an obvious extension of this, but I personally don't care about it as much.
It makes sense if you are using a FileLoader. But what if you have a custom loader which loads templates from database ? Or if you have a ServletLoader ?
It only applies for FileLoader I think
fair, perhaps, though see my follow up to #556, I'm not certain about the servlet loader, but due to the fact that you in theory could use a FileLoader in conjunction with another loader, if you did this.
even if this isn't done, could we improve the error message when using file loader to give the absolute path? at first I didn't realize my CWD was at the root of my repo (running via IDE), because my application/gradle isn't.
also, could StringLoader be added as an example in the docs? I did look for it. side question about docs, is there any kind of special array index/map key access? e.g list[i] map[key]?
Yes. You can access a map entry as a field (map.someKey or if you want map["someKey"] or dynamically map[keyFromVariable]), this is documented at https://pebbletemplates.io/wiki/guide/basic-usage/#variables. The documentation doesn't say it but I just checked the code and List values can be accessed like arrays. (com.mitchellbosecke.pebble.attributes.ListResolver) I've created a patch for the documentation at #563.
If foo is a map, foo.get("bar")
suggests that you have to do foo.get("bar") and foo["bar"] won't work in that case.
I've created a patch for the documentation at #562.
cool.
Behind the scenes foo.bar will attempt the following techniques to to access the bar attribute of the foo variable: