Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you need to rethink your Mustache template slightly. The jMustache library (which I assume you are using) seems to always treat <code>{{#</code> entities as lists and iterate their contents, regardless of the data type passed in.</p> <p>Something like this should work:</p> <pre><code>&lt;h1&gt;{{header}}&lt;/h1&gt; {{#items}} &lt;li&gt; {{#url}}&lt;a href="{{.}}"&gt;{{/url}} {{^url}}&lt;strong&gt;{{/url}} {{caption}} {{#url}}&lt;/a&gt;{{/url}} {{^url}}&lt;/strong&gt;{{/url}} &lt;/li&gt; {{/items}} {{^items}} &lt;p&gt;The list is empty.&lt;/p&gt; {{/items}} </code></pre> <p>This will produce an HMTL anchor only if a "link" value is provided, thus avoiding the jMustache if condition issue. So the JSON model would look something like this:</p> <pre><code>{ "header": "Colors", "items": [ {"caption": "title"}, {"caption": "red", "url": "#Red"}, {"caption": "green", "url": "#Green"}, {"caption": "blue", "url": "#Blue"} ] } </code></pre> <p>Finally, you will need to convert your JSON to something jMustache understands. I've never seen or heard of the "HTTPFunctions" class in any library I've used, but I've done some similar mapping using Gson in the past. Note that this is a very simple implementation and you may need to extend it to fit your needs:</p> <pre><code>private Map&lt;String, Object&gt; getModelFromJson(JSONObject json) throws JSONException { Map&lt;String,Object&gt; out = new HashMap&lt;String,Object&gt;(); Iterator it = json.keys(); while (it.hasNext()) { String key = (String)it.next(); if (json.get(key) instanceof JSONArray) { // Copy an array JSONArray arrayIn = json.getJSONArray(key); List&lt;Object&gt; arrayOut = new ArrayList&lt;Object&gt;(); for (int i = 0; i &lt; arrayIn.length(); i++) { JSONObject item = (JSONObject)arrayIn.get(i); Map&lt;String, Object&gt; items = getModelFromJson(item); arrayOut.add(items); } out.put(key, arrayOut); } else { // Copy a primitive string out.put(key, json.getString(key)); } } return out; } </code></pre> <p>This basic JUnit test demonstrates the theory: <a href="http://www.pasteshare.co.uk/p/841/" rel="nofollow">http://www.pasteshare.co.uk/p/841/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload