Recursive Partials

109 views
Skip to first unread message

Lee Theobald

unread,
Jul 23, 2015, 8:24:35 AM7/23/15
to handlebars.java
Hi all,

I was wondering if someone could tell me if I'm getting my recursive code right. I'm getting a lot of infinite loop warnings when I think my code should be fine.

I have a model class that looks like:

public class Group {
private String id;
private List<Group> groups;

// Followed by constructors, getters, setters etc.
}

I set up a tree structure using these groups & try the following partial:

{{#each groups}}
<div class="panel panel-default">
<div class="panel-heading">{{ id }}</div>
<div class="panel-body">
{{#if groups}}
{{> _group this }}
{{/if}}
</div>
</div>
{{/each}}

But the recursive partial include causes me the trouble. To me the {{if}} should prevent me from any infinite loops but I'm guessing it's not checking the groups property of the current object but of the one I've passed in to the function.

Could someone give me some guidance on getting this working? I've got an example on GitHub if you want to run something: https://github.com/LTheobald/RecursiveHandlebarsTest. It's a Spring Boot project so just run the main class in ApplicationLauncer & it should start a local Tomcat. Then visit http://localhost:8080 in the browser.

Cheers for any help,
Lee

edgar

unread,
Jul 23, 2015, 8:29:06 AM7/23/15
to handlebars.java, lee.th...@gmail.com, lee.th...@gmail.com
It's a check handlebars.java has enabled by default.

Try: handlebars.setInfiniteLoop(true);

That will do it!

Also, I invite you to try: http://jooby.org/ a more lightweight alternative to Spring Boot :)

Lee Theobald

unread,
Jul 23, 2015, 9:09:20 AM7/23/15
to handlebars.java, espina...@gmail.com
That's fixed it. Thanks a bunch :)

Lee,

Matt A

unread,
Aug 9, 2016, 7:23:09 PM8/9/16
to handlebars.java
Hi Guys,

I know this post is old, but don't want to start a new thread since I'm at a loss with recursive partials.


We have data setup in the following structure:

class Group {
List<Item> items;
}

class Item {
List<Item> children;
}

Our template is setup as follows:
<!-- Main Template -->
{{#grp}}
{{#each items}}
{{> tplItem}}
{{/each}}
<hr style="width:30%; border-color: black;">
{{/grp}}

<!-- Partial Item Template (tplItem.html) -->
<div>
{{itemDescription}}
....
</div>

{{#if children}}
{{#each children}}
{{> tplItem}}
{{/each}}
{{/if}

We've set InfiniteLoop(true), but are getting a stack overflow with the following called repeatedly.

at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:128)
at com.github.jknack.handlebars.internal.TemplateList.merge(TemplateList.java:94)
at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:128)
at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:116)
at com.github.jknack.handlebars.Options.apply(Options.java:538)
at com.github.jknack.handlebars.Options.fn(Options.java:433)
at com.github.jknack.handlebars.helper.IfHelper.apply(IfHelper.java:53)
at com.github.jknack.handlebars.internal.Block.merge(Block.java:211)
Reply all
Reply to author
Forward
0 new messages