Boolean objects

160 views
Skip to first unread message

Kiran Hampal

unread,
Jun 25, 2019, 6:36:22 PM6/25/19
to mustache.java
Hello,

I've got a JAXB object with some optional Boolean object fields.
In my mustache template I want to achieve the following behavior:

Boolean is true -> print "made up true string here"
Boolean is false -> print "made up false string here"
Boolean is null/not set -> don't print anything

For something like an integer I'd just do this:

{{#jaxbobject.item}}made up {{jaxbobject.item}} string here{{/jaxbobject.item}}

However in this case, both false and null values of the boolean will exhibit the same behaviour. :(

What's the recommended way of achieving the desired behaviour mustache? Is there a way of customising mustache to introduce my own "not null/null" operator or do I need to store each string for each field in my context (the jaxb objects are quite big!).

Thanks
Kiran

Sam Pullara

unread,
Jun 25, 2019, 8:01:43 PM6/25/19
to mustac...@googlegroups.com
There isn't really support for 3 valued booleans in the language. Can you craft a very specific small example of what your template would look like and what the expected output would be for the 3 cases?


--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mustachejava/67ef4522-1010-4d56-be5b-becdb053b378%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiran Hampal

unread,
Jun 25, 2019, 8:34:01 PM6/25/19
to mustache.java
Hi Sam

In the JAXB object (loaded from xml)

public class ExampleBlock {

   ...

   private Boolean setXOptionEnabled; // (and associated getter/setter)

   ...

}

In the mustache template (which generates a java file - it's part of a code generation project) it should ideally output:

On true:

template.setSpecificPropertyEnabled(true);

On false:

template.setSpecificPropertyEnabled(false);

On null, there shouldn't be anything printed. This is because the 'template' object is third party (not written by me) and sets its own default if the setter isn't called on that object.

Thanks
Kiran


On Wednesday, 26 June 2019 01:01:43 UTC+1, Sam wrote:
There isn't really support for 3 valued booleans in the language. Can you craft a very specific small example of what your template would look like and what the expected output would be for the 3 cases?


On Tue, Jun 25, 2019 at 3:36 PM, Kiran Hampal <kiran....@gmail.com> wrote:
Hello,

I've got a JAXB object with some optional Boolean object fields.
In my mustache template I want to achieve the following behavior:

Boolean is true -> print "made up true string here"
Boolean is false -> print "made up false string here"
Boolean is null/not set -> don't print anything

For something like an integer I'd just do this:

{{#jaxbobject.item}}made up {{jaxbobject.item}} string here{{/jaxbobject.item}}

However in this case, both false and null values of the boolean will exhibit the same behaviour. :(

What's the recommended way of achieving the desired behaviour mustache? Is there a way of customising mustache to introduce my own "not null/null" operator or do I need to store each string for each field in my context (the jaxb objects are quite big!).

Thanks
Kiran

--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustac...@googlegroups.com.

Sam Pullara

unread,
Jun 26, 2019, 1:53:21 PM6/26/19
to mustac...@googlegroups.com
Generally you should be using views on top of your objects rather than the data objects directly so that you could have a method that tells you whether or not something is set — in mustache, things are either truthy or falsey. There is no native way to put this kind of business logic into the template. You could potentially add this kind of thing to your ObjectHandler but you're going down a distinctly non-mustache path at that point. I talk a little bit about avoiding using data objects directly as inputs to a template for this reason:


Sam

To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mustachejava/d2cc0c45-5938-4bcb-9fe5-c5f87fb051f1%40googlegroups.com.

Michael Hixson

unread,
Jun 26, 2019, 2:17:53 PM6/26/19
to mustac...@googlegroups.com
Not saying this is a good idea, but you could abuse toString to distinguish Boolean.FALSE from null.

only true: {{#booleanField}}YES{{/booleanField}}
only false: {{^booleanField}}{{#booleanField.toString}}YES{{/booleanField.toString}}{{/booleanField}}
only null: {{^booleanField.toString}}YES{{/booleanField.toString}}

true or false: {{#booleanField.toString}}YES{{/booleanField.toString}}
false or null: {{^booleanField}}YES{{/booleanField}}

-Michael

Kiran Hampal

unread,
Jun 27, 2019, 1:14:40 PM6/27/19
to mustache.java
Thanks both for your replies :)

Sam, I agree and understand with what you're saying. Unfortunately it's going to mean a lot of duplication to some classes which are 100s of fields big. :( I might just do it though

Michael, that's a really neat (if slightly hacky) trick! That would also help with distinguishing between non empty, empty and null lists.


On Wednesday, 26 June 2019 19:17:53 UTC+1, Michael Hixson wrote:
Not saying this is a good idea, but you could abuse toString to distinguish Boolean.FALSE from null.

only true: {{#booleanField}}YES{{/booleanField}}
only false: {{^booleanField}}{{#booleanField.toString}}YES{{/booleanField.toString}}{{/booleanField}}
only null: {{^booleanField.toString}}YES{{/booleanField.toString}}

true or false: {{#booleanField.toString}}YES{{/booleanField.toString}}
false or null: {{^booleanField}}YES{{/booleanField}}

-Michael

On Wed, Jun 26, 2019 at 10:53 AM Sam Pullara <spul...@gmail.com> wrote:
Generally you should be using views on top of your objects rather than the data objects directly so that you could have a method that tells you whether or not something is set — in mustache, things are either truthy or falsey. There is no native way to put this kind of business logic into the template. You could potentially add this kind of thing to your ObjectHandler but you're going down a distinctly non-mustache path at that point. I talk a little bit about avoiding using data objects directly as inputs to a template for this reason:


Sam

--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustac...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages