0

Guys I am stuck in displaying a base64 image in a HTML e-mail using handlebars.js.

    //imageURL : data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==
    <img id="img" class="someClass" src="{{imageURL}}" >

I don't know how to display a image using handlebars. Kindly help me to move further.

gerard
  • 174
  • 1
  • 4
  • 17
tamilmani
  • 591
  • 6
  • 13

2 Answers2

1

Your template is already correct. If you want an example using your template see the following code:

var source = document.getElementById("template").innerHTML;
var template = Handlebars.compile(source);
var context = {
  imageURL: "data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw=="
};
var html = template(context);

document.getElementById("template").innerHTML = html;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.min.js"></script>
<div id="template">
  <img id="img" class="someClass" src="{{imageURL}}" >
</div>

Update: The author didn't specify that the template was used in a e-mail. As said in the comments it is possible that the e-mail client doesn't support base64 images. For a more detailed answer about this, check the answer at http://stackoverflow.com/a/9330720/2540618.

gerard
  • 174
  • 1
  • 4
  • 17
  • when its send to mail as a html template it does not work,actually am trying to send a image to mail as a html template using handlebar – tamilmani Apr 20 '18 at 09:52
  • Some e-mail clients wont support this. See the following answer on a different question on StackOverflow which e-mail clients support Image URI's: https://stackoverflow.com/a/9330720/2540618 – gerard Apr 20 '18 at 13:23
1

You need to use SafeString from Handlebars like this:

import { SafeString } from 'handlebars'

const imageURL = new SafeString('data:image/png;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACw...')

// In your template
<img id="img" class="someClass" src="{{imageURL}}" >
Dharman
  • 30,962
  • 25
  • 85
  • 135
Miguel Carbajal
  • 311
  • 3
  • 4