简体   繁体   English

HTML无法呈现-Express

[英]HTML is not rendering - Express

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 . 当我尝试在帖子中使用HTML标签时,看到它们没有呈现, 其余页面就可以了

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'); 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: 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? 为什么HTML无法呈现?

(Actually I'm not sure this is Express question, I'm new to Web development.) (实际上,我不确定这是Express问题,还是Web开发的新手。)


Solution: 解:

Based on Andrew Counts answer I used three brackets and problem is solved. 基于Andrew Counts的答案,我使用了三个括号,并解决了问题。 {{{...}}}

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. 在车把模板中,HTML代码会自动从变量中转义,以确保存储的值不会破坏页面代码流。 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`. 如果您确定要输出存储在变量中的HTML,并且不会破坏页面,则可以使用三括号{{{ }}}或通过将值存储为“ Handlebars.SafeString”类型。

http://handlebarsjs.com/ http://handlebarsjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM