简体   繁体   中英

handlebars won't render HTML image tag? (using NodeJS)

I'm trying to render an HTML image tag with handlebars. The HTML does get passed to handlebars properly. (So does get passed to the webpage, but the image is not rendered.) How do I fix this?

In my .HBS file I have:

{{HTMLforImage}} {{image}} {{alt}}

and in my .js file I have:

  res.render('imageHub', {
  HTMLforImage: "<img src = ",
  image: "\"" + req.body.imageLink + "\"",
  alt: " alt = \"raccoon\">"
});

where imageHub is the page handlebars should render the HTML, but instead of a picture being posted I am just getting the HTML being passed onto the webpage. Any ideas on how to fix this would be really appreciated.

{{HTMLforImage}} will escape markup. Try triple brackets: {{{HTMLforImage}}}

Use triple braces to escape HTML special characters such as the < and > characters:

{{HTMLforImage}} {{image}} {{alt}}

Also you have <image ... but it should be <img ...

The best practice would be to change your handlebars content to:

<img src="{{imageLink}}" alt="{{alt}}" />

and pass only the required data like this:

res.render('imageHub', {
  imageLink: req.body.imageLink,
  alt: 'raccoon'
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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