0

My situation is as follows:

I receive an object from an AJAX call which contains an array of strings (not key/value pairs). It would be amazing if I could just take that array and toss it into the template's data object like the below, and then recursively show each value in the array via the ICanHaz mustache section template syntax. Unfortunately I can not get it to work without key/value pairs to reference inside the section template.

This is a major pain because I have to go into the array returned from AJAX and add key value pairs.

I could not find anything on the mustache or ICanHaz.js documentation dealing with this matter.

Javascript:

var listOfStuff = {
    name: 'This is not important',
    stuff: data.ajaxreturnedarray || [1, 2, 3, 4]  // Just for example.
};

$("#new").append(ich.test(listOfStuff));

HTML

<div id="new">Test</div>

<script type="text/html" id="test">
<div class="panel">
    {{#stuff}}<p>{{stuff}}</p>{{/stuff}}
    {{^stuff}}<p>No items :(</p>{{/stuff}}
</div>
</script>

Does anyone know a way to make this work?


Update

This is covered in this answer, but it does not mention ICanHaz.js so I cross-reference for anyone googling this problem with ICanHaz.js without mustache.

1 Answer 1

1

AFAIK, ICanHaz embeds Mustache, and if it's Mustache you can simply do

{{#stuff}}<p>{{.}}</p>{{/stuff}}
Sign up to request clarification or add additional context in comments.

2 Comments

works like a charm thank you. Is this somewhere in the Mustache documentation?
Yes, under the paragraph "Non-empty lists".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.