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

lookup is not implemented correctly #1035

Open
segevfiner opened this issue Dec 5, 2022 · 1 comment
Open

lookup is not implemented correctly #1035

segevfiner opened this issue Dec 5, 2022 · 1 comment

Comments

@segevfiner
Copy link

segevfiner commented Dec 5, 2022

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 segevfiner changed the title lookup is not implemented correctly lookup is not implemented correctly Dec 5, 2022
@segevfiner
Copy link
Author

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.

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

1 participant