I was trying to render a mustache template which is referencing itself in it. But it gives a ‘Stack level too deep’ error.
Here is my code in ruby.
The following snippet of code is in person.rb file
require 'mustache'
require 'active_support'
str = File.read("person.json")
j = ActiveSupport::JSON.decode(str)
Mustache.template_file = "person.mustache"
puts Mustache.render(j)
The following json content is in person.json
{
"name":"Jason",
"rels":[
{"type":"friend",
"ref":{
"name":"John",
"rels":[
{"type":"friend",
"ref":{"name":"Chrissy"}}
]
}},
{"type":"family",
"ref":{"name":"Owen"}}
]
}
The following content is in the file person.mustache file
{{#rels}}
<ul>
<li>Type: {{type}}</li>
{{#ref}} {{> person}} {{/ref}}
</ul>
{{/rels}}
can someone point me in the right direction?
From the fine manual:
So, if there is no
relsin the current context:then you’ll inherit the
relsfrom the parent. That gives you a partial referencingrelsfrom the parent which activates the partial again which referencesrelsfrom the parent which keeps going until you run out of stack.If you’re going to build a recursive template like this:
then you need to have complete objects at each level:
You can flesh out your data in the JSON or after you parse it, I’d recommend that you flesh it out after parsing it.