nchopra nchopra - 6 months ago 13
Javascript Question

How to render block of 'escaped' HTML returned in JSON response

I'm a beginner programmer trying to make a simple Meteor app whereby I can display data from another site of mine through its REST API (the site runs on Joomla and I'm using jBackend for the REST API - however this is just context and doesn't apply to the question)

When I send a GET request and receive a response, the JSON returned gives me the content of my article as a huge HTML block -

{
"status": "ok",
"id": "23",
"title": "The Ivy",
"alias": "the-ivy2",
"featured": "0",
"content": "<div class=\"venue-intro\">\r\n\t<h1>\r\n\t\t\t\t\t<a href=\"index.php?option=com_content&view=article&id=23:the-ivy2&catid=14:leisure-activites\" title=\"The Ivy\">\r\n\t\t\t\t\tThe Ivy\r\n\t\t\t\t\t</a>\r\n\t\t\t</h1>\r\n\r\n\t<div class=\"row\">\r\n\t\t<div class=\"col-md-5\">\r\n\t\t\t\t\t\t\t<div class=\"venue-intro-img\">\r\n\t\t\t\t\t\t\t\t\t\t\t<a href=\"index.php?option=com_content&view=article&id=23:the-ivy2&catid=14:leisure-activites\" title=\"The Ivy\">\r\n\t\t\t\t\t\t\t\t\t\t\t<img src=\"http://localhost/stc/images/stories/com_form2content/p4/f20/thumbs/theIvy.jpg\">\r\n\t\t\t\t\t\t\t\t\t\t\t</a>\r\n\t\t\t\t\t\t\t\t\t</div>\r\n\t\t\t\t\t</div>\r\n\t\t<div class=\"col-md-7\">\r\n\t\t\t\t\t\t\t<p><p>The Ivy is situated in the heart of London's West End, close to Leicester Square, Shaftsbury Avenue and the vibrant quarters of Covent Garden and Soho. Open late each night, the restaurant is...</p>\r\n\t\t\t\t\t</div>\r\n\t</div>\r\n</div><div class=\"venue-main\">\r\n<h1>The Ivy</h1>\r\n\t<img src=\"http://localhost/stc/images/stories/com_form2content/p4/f20/theIvy.jpg\" class=\"venue-main-img\">\r\n\t<p><p>The Ivy is situated in the heart of London's West End, close to Leicester Square, Shaftsbury Avenue and the vibrant quarters of Covent Garden and Soho. Open late each night, the restaurant is also perfect for post theatre dinner. The Royal Opera House, Coliseum and all other theatres are a short walk away. The space can be arranged and dressed to suit your event- whatever the style and can accommodate 25-120 people.</p>\r\n<p>The room comes with a baby grand piano, fresh flowers, candles and place cards. AV equipment and musicians can be arranged and our event production company, Urban Caprice, can re-design and style the room for any event, supplying props, lighting and much more. We create seasonal menus especially for the Private Room, but let us know if you have any other favourite dishes and we'd love to try and include them.</p>\r\n<p><a href=\"http://www.the-ivy.co.uk/\" target=\"_blank\">http://www.the-ivy.co.uk/</a></p></p>\r\n</div>"
}


I am trying to render this block as is on my app, but I can't manage it - here's what I've been trying so far -

Template.articles.helpers({
'content': function() {
return $('<div />').html(this.content).text();
}
});


Using this method gives me this output -

<div class="venue-intro"> <h1> <a href="index.php?option=com_content&amp;view=article&amp;id=23:the-ivy2&amp;catid=14:leisure-activites" title="The Ivy"> The Ivy </a> </h1> <div class="row"> <div class="col-md-5"> <div class="venue-intro-img"> <a href="index.php?option=com_content&amp;view=article&amp;id=23:the-ivy2&amp;catid=14:leisure-activites" title="The Ivy"> <img src="http://localhost/stc/images/stories/com_form2content/p4/f20/thumbs/theIvy.jpg"> </a> </div> </div> <div class="col-md-7"> <p></p><p>The Ivy is situated in the heart of London's West End, close to Leicester Square, Shaftsbury Avenue and the vibrant quarters of Covent Garden and Soho. Open late each night, the restaurant is...</p> </div> </div> </div><div class="venue-main"> <h1>The Ivy</h1> <img src="http://localhost/stc/images/stories/com_form2content/p4/f20/theIvy.jpg" class="venue-main-img"> <p></p><p>The Ivy is situated in the heart of London's West End, close to Leicester Square, Shaftsbury Avenue and the vibrant quarters of Covent Garden and Soho. Open late each night, the restaurant is also perfect for post theatre dinner. The Royal Opera House, Coliseum and all other theatres are a short walk away. The space can be arranged and dressed to suit your event- whatever the style and can accommodate 25-120 people.</p> <p>The room comes with a baby grand piano, fresh flowers, candles and place cards. AV equipment and musicians can be arranged and our event production company, Urban Caprice, can re-design and style the room for any event, supplying props, lighting and much more. We create seasonal menus especially for the Private Room, but let us know if you have any other favourite dishes and we'd love to try and include them.</p> <p><a href="http://www.the-ivy.co.uk/" target="_blank">http://www.the-ivy.co.uk/</a></p><p></p> </div>


That looks like perfectly valid HTML yet the problem is that the browser isn't rendering it as such - it just outputs this long string literally. :(

At the end of the day, I'd like to be able to render the HTML that I'm receiving in my JSON response.

Any help is appreciated.

Answer

Blaze has a way to render raw HTML strings using triple curly braces instead of the normal two.

Having

{{{content}}}

in your template should render your content, provided the helper returns valid HTML.

Be very careful when using it, especially if it contains user-generated content.

Comments