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
Syntax when we have space in the JSON key #508
Comments
Hello, Thank you, for being a handlebars.java user :) Unfortunately, don't get the problem. Can you explain better with some example or reason of why the Thanks |
Hello, Thanks |
Oh I see, there is a hacky way that you will have to test and probably improve: public class Workaround extends v4Test {
@Override
protected void configure(final Handlebars handlebars) {
handlebars.registerHelperMissing(new Helper<String>() {
@Override
public Object apply(final String name, final Options options) throws IOException {
// template to property path, so {{first name}} become [first name]
String raw = options.fn.text();
String path = raw.replace("{{", "[").replace("}}", "]");
return options.context.get(path);
}
});
}
@Test
public void test() throws IOException {
shouldCompileTo("{{first name}}", $("hash", $("first name", "Santosh Kumar")), "Santosh Kumar");
}
} So, here handlebars try to find a As I said, this is a workaround and you might ran into troubles if there is an existing helper with the same name as the attribute... Still, don't think that is a real problem if you come from mustache.java but you will have to play. Let me know if you have questions |
Did you try it? Did it work? |
Hi,
Thanks. |
Sorry, that was just an example using the handlebars test framework. Here is the example in plain/raw handlebars: Handlebars handlebars = new Handlebars();
handlebars.registerHelperMissing(new Helper<String>() {
@Override
public Object apply(final String name, final Options options) throws IOException {
// template to property path, so {{first name}} become [first name]
String raw = options.fn.text();
String path = raw.replace("{{", "[").replace("}}", "]");
return options.context.get(path);
}
}); Give it a try! |
Hi, Thanks |
My bad, the snapshot/master version returns an Object while the latest Maven release returns a CharSequence. I will make a release soon. In production environment, you need to use a cache. I recommend you the Guava Cache Thanks. |
Hi,
exception. In the same way can you have any solution Thanks |
I need an example of when you got a HandlebarsException |
In JSON object key like {{first & name}}. In the case of special characters... |
Sorry, but there is no workaround for that in handlebars.java. It is probably better to rewrite the template and then call handlebars via regex or similar. So you make sure every single {{...}} expression is translated to {{[...]}}. Think is the safe way and you don't need to the helper missing hack. |
Hi jknack,
Thank you for your plugin.
We are trying to get the data of JSON like {{first name}}. You given syntax as {{[first name]}} but this we can't able to implement in our app. Is there any proper solution for this?
Thanks & Regards,
Santosh Kumar
The text was updated successfully, but these errors were encountered: