0

I'm making a simple blog app for my site and fetching posts from array. When I try to use HTML tags in the posts I'm seeing they are not rendering, rest of page is ok.

Posts:

{"post_id":1, 
 "post_title":"Title", 
 "post_body":"This is a test text. <b> I'm testing HTML.</b> "} ....

Output in the page is :

This is a test text. <b> I'm testing HTML.</b>

Server-side:

var hbs = require('hbs');

// view engine setup
app.set('view engine', 'html');
app.engine('html', hbs.__express);

app.get('/post/:id', function(req, res) {
    res.render('post_theme',
    {post, title etc...});
    });

HTML:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <title>Page</title>
  <!-- font css -->
  <link href='http://fonts.googleapis.com/css?family=Open+Sans:300,400' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="/lib/semantic/build/packaged/css/semantic.css">
  <link rel="stylesheet" type="text/css" href="/stylesheets/style.css">
  <!-- js -->
  <script src="/javascripts/jquery.js"></script>
  <script src="/lib/semantic/build/packaged/javascript/semantic.js"></script>


</head>
<body id="home">
 <div class="ui menu" >
....
</div>

        <h3 class="ui header">{{title}}</h3>
        <p>{{body}}</p>

</body>


</html>

Why HTML is not rendering?

(Actually I'm not sure this is Express question, I'm new to Web development.)


Solution:

Based on Andrew Counts answer I used three brackets and problem is solved. {{{...}}}

9
  • did you create the project by using the cli interface? i.e. 'express install xxxx' ?? Commented Aug 18, 2014 at 16:31
  • Yes, I created it with express-io framework from command line. Commented Aug 18, 2014 at 16:32
  • 1
    Your question is confusing. First you say that HTML is not rendering, but then you say that the rest of the page is ok. EVERYTHING that renders in a browser is HTML, so you need to be a bit more specific about what you expect to render, and what actually is.... you are certainly getting SOME HTML, unless a blank white screen is what you consider ok. Commented Aug 18, 2014 at 16:32
  • 1
    ok based on your edit, I assume what you mean is that you have HTML tags in your data model, and you want them rendered as HTML instead of as plain text. Commented Aug 18, 2014 at 16:35
  • 2
    Try using triple curly braces to access the raw html, {{{body}}} Commented Aug 18, 2014 at 16:40

1 Answer 1

4

In handlebars templates, HTML codes are escaped from variables automatically, in order to ensure that values stored are not able to break the page code flow. if you are SURE that you do want to output the HTML stored in a variable, and that it will not break your page, you can instruct Handlebars to not escape the HTML, either by using triple braces {{{ }}}, or by storing the value as type 'Handlebars.SafeString`.

http://handlebarsjs.com/

Sign up to request clarification or add additional context in comments.

Comments

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.