I have a simple JSON file as shown below:
{ "products": [ { "title": "United Colors of Benetton Men Shirt", "description": "Cool, breezy and charming β this solid green shirt from United Colors of Benetton is born on the beach. Effortlessly classy, this full sleeved shirt is perfect when worn with faded blue jeans and a pair of shades for a weekend get-together.", "quantity": "10", "cost": "3.00", "brand": "United", "image": "catalog/images/img2.jpg", "category": "1", "popularity": "100" } ] }
I show this JSON file using Mustache.js in a template hit:
<table class="product-list"> {{#products}} <tr> <td> <table class="product"> <tr> <td class="product-image"> <img src"{{image}}" height="150" width="150" /> </td> <td class="product-details"> <p class="title">{{title}}</p> <p class="description">{{description}}</p> <p class="quantity"><b>Quanity Available: </b>{{quantity}}</p> <p class="cost"><b>Cost: </b>£ {{cost}}</p> <p class="brand"><b>Brand:</b> {{brand}}</p> </td> </tr> </table> </td> </tr> {{/products}} </table>
Everything works fine, but for some reason, the slash in the image property is escaped, which is why the images are not displayed.
I tried to snatch slashes in a JSON file by adding a backslash in front of them. But instead of the right way, I get this.
catalog\/images\/img2.jpg
I am also trying to disable HTML escaping using {{{image}}} and I get this.
catalog\="" images\="" img2.jpg=\""
How can I display the image property correctly?
Can anyone help me with this?
Edit: JS is used to create the template:
$template = $('#product-template').html(); $renderedHtml = Mustache.render($template, $data); $('#content').html($renderedHtml);
json javascript mustache
Jay bhatt
source share