handlebars.java
handlebars.java copied to clipboard
`lookup` is not implemented correctly
lookup is supposed to search for the key literal value, but stuff like dots in it takes effect:
{
"foo.js": {
}
}
{{lookup . "foo.js"}}
Fails while it should work.
While:
{{lookup . "[foo.js]"}}
Does work, but that's not how lookup in handlebars.js behaves.
I'm also not sure about the behavior in the case of a key that is not found.
Well... I did this in my project for now:
/**
* Lookup helper like [com.github.jknack.handlebars.helper.LookupHelper], but with a bug fix for <https://github.com/jknack/handlebars.java/issues/1035>
*/
private object LookupHelper : Helper<Any?> {
@Throws(IOException::class)
override fun apply(context: Any?, options: Options): Any? {
if (context == null) {
return null
}
if (options.params.isEmpty()) {
return context
}
val ctx = Context.newBuilder(options.context, context).build()
return ctx["[${options.param<Any>(0).toString()}]"]
}
/**
* The helper's name.
*/
const val NAME = "lookup"
}
In Kotlin, but can be easily converted to Java.