Call functions and support for break

103 views
Skip to first unread message

Burak Emre Kabakcı

unread,
Apr 16, 2018, 8:29:51 PM4/16/18
to mustache.java
I need to generate the following output with Mustache:

  least("event0_count")
  least("event0_count", "event1_count")

I just have the list ("event1", "event2") in my scope, the size of the list is dynamic and I need to be able to generate this string using Mustache in order to be able to use it in our project. I'm kinda stuck here.

There are two different ways that I found and none of them worked.

1. Use inner loops and the and let the inner loop call the function `IntStream.range(0, sizeOfList)` but function calls are not allowed in Mustache.
2. Use inner loops and break in inside of the inner loop but the `break` feature is not supported in Mustache.java

Is there any workaround for this output or do you have any other suggestion?

Sam Pullara

unread,
Apr 17, 2018, 1:23:36 PM4/17/18
to mustac...@googlegroups.com
You need to use a view class on top of your collection. There is one included in Mustache called DecoratedCollection that includes things like first/last/etc. Here is a link to a test that renders things like I think you want:


See the "testArrayOutput" test for I think your exact use case.

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 mustachejava+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Burak Emre Kabakcı

unread,
Apr 18, 2018, 9:10:46 AM4/18/18
to Sam Pullara, mustac...@googlegroups.com
Hey Sam,

Thanks for the response, I’m aware of DecoratedCollection but not sure how it can solve this problem. I’m able to use first, last and index variables and since I have an inner loop, the loop in inside overrides the values of index, first and last. Let’s consider this example:

{{#steps}} 
least({{#steps}} event{{index}}_count{{^last}},{{/last}} {{/steps}}){{^last}},{{/last}} 
{{/steps}}

The output is the following:

least( event0_count,  event1_count,  event2_count ),
least( event0_count,  event1_count,  event2_count ),
least( event0_count,  event1_count,  event2_count )

However I need to generate the following:

least( event0_count),
least( event0_count,  event1_count ),
least( event0_count,  event1_count,  event2_count )

It may be possible to render if I can access the parent scope and check if the child index is less than the parent index like this one:

{{#steps}} 
least({{#steps}} {{#parent.index < child.index}} event{{index}}_count{{^last}},{{/last}} {{/parent.index < child.index}} {{/steps}}){{^last}},{{/last}} 
{{/steps}}

However this template is not valid so I’m not able to do it.

Best,
Emre
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava...@googlegroups.com.

Sam Pullara

unread,
Apr 18, 2018, 3:24:19 PM4/18/18
to Burak Emre Kabakcı, mustac...@googlegroups.com
Ah, I didn't catch that it was a nested loop. For this you will have to have a view class that returns a truncated list since there is no way to break out of it. Mustache was designed around generating things like HTML that don't need this kind of behavior. I've added a test case that shows how to get the output you are looking for.


Sam

Reply all
Reply to author
Forward
0 new messages