handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

`lookup` is not implemented correctly

Open segevfiner opened this issue 3 years ago • 1 comments

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.

segevfiner avatar Dec 05 '22 16:12 segevfiner

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.

segevfiner avatar Dec 05 '22 16:12 segevfiner