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

Syntax when we have space in the JSON key #508

Closed
santhosh0101 opened this issue Jun 8, 2016 · 12 comments
Closed

Syntax when we have space in the JSON key #508

santhosh0101 opened this issue Jun 8, 2016 · 12 comments
Labels
Milestone

Comments

@santhosh0101
Copy link

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

@jknack
Copy link
Owner

jknack commented Jun 8, 2016

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 {{[first name]}} syntax doesn't work for you?

Thanks

@santhosh0101
Copy link
Author

Hello,
Thank you for responding. Actually we implemented mustache.java in our app. Now we migrated to Handlebars.java.
Previously they(mustache) allowed space in the JSON key. With normal syntax. All our customers are using that only. But now it is different in your plugin. We can't able to change that now. That's why we need proper solution to use in previous manner.

Thanks

@jknack
Copy link
Owner

jknack commented Jun 8, 2016

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 first helper, the key is the missingHelper feature that works as fallback solution. There what I do is to convert the template expression from {{first name}} to [first name] and ask context to resolve the value.

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

@jknack jknack added the question label Jun 8, 2016
@jknack
Copy link
Owner

jknack commented Jun 8, 2016

Did you try it? Did it work?

@santhosh0101
Copy link
Author

santhosh0101 commented Jun 9, 2016

Hi,

  I tried it. But it showing error while using  Object apply. Is there any special jar file I have to include for this?

Thanks.

@jknack
Copy link
Owner

jknack commented Jun 9, 2016

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!

@santhosh0101
Copy link
Author

santhosh0101 commented Jun 9, 2016

Hi,
Its working fine But when i changed Object to CharSequence and returning string. I don't know why it is not accepted.
I am using compileInline. But it is taking little bit more time. Are you providing any other way to compile to reduce compilation time? You mentioned precompile using handlebars.js. But can we do it for handlebars.java.

Thanks

@jknack
Copy link
Owner

jknack commented Jun 9, 2016

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.

@santhosh0101
Copy link
Author

santhosh0101 commented Jun 9, 2016

Hi,

   Thank you jknack. You gave me better solution for performance as well as HelperMissing

exception. In the same way can you have any solution
for HandlebarsException?
Because when we use some special
characters and international characters this error is coming.

Thanks

@jknack
Copy link
Owner

jknack commented Jun 9, 2016

I need an example of when you got a HandlebarsException

@santhosh0101
Copy link
Author

In JSON object key like {{first & name}}. In the case of special characters...

@jknack
Copy link
Owner

jknack commented Jun 9, 2016

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.

@jknack jknack added this to the 4.0.7 milestone May 28, 2018
@jknack jknack closed this as completed May 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants