Handlebar Java : Adding OR condition in if statement

2.3k Views Asked by At

How can I add an OR condition in handlebar using java? I want to do something like:

{{#if condition1}} || {{#if condition2}}
  do something
  .....
  ......
{{else}}
  do something else...
{{/if}}

Do I need to register some helper? For example for comparing if one number is greater than other, I have written a helper like:

handlebars.registerHelper("ifgt", new Helper<Integer>() {
            @Override
            public CharSequence apply(Integer value, Options options) throws IOException {
                if (value == null || options.param(0) == null) return options.inverse();
                if (value.compareTo(options.param(0)) > 0) {
                    return options.fn();
                }
                return options.inverse();
            }
        });
2

There are 2 best solutions below

0
On

Handlerbars for Java (since v4 I think) comes with a support class ConditionalHelpers which provides conditional logic operators, although you still have to register these before they can be used.

for example

{{#if (and (not moreTransaction) (or (eq pageNumber 1) (not (gt previousCount 8))) (gt rowsRemaining 8))}}
...
0
On

Finally found a library for basic operations like or,and,not,mathematical operations in handlebar java.

Refer: http://handlebars-java-helpers.beryx.org/releases/latest/#_or

For usage with java : https://github.com/beryx/handlebars-java-helpers/issues/1#issuecomment-355653085