简体   繁体   中英

How to store a template in an expression in Handlebars.js?

I want to place my templates on several places in the DOM without having to write the jQuery expression over and over again. I want to place my template in the DOM by just typing the variable/attribute {{template}} anywhere in the body.

Is this doable using Handlebars? If not, is there any other way for me to achieve this?

You need to use HandleBar helpers

handlebar_helpers
ps dont forget to use safestring else it will be escaped by default



Updating with answer based on ur comment The link i provided did give out an excellent explanation. However please find below an example.
Assume your need to return a welcome message when a name is given

Handlebars.registerHelper('welcome', function(name) {
  return new Handlebars.SafeString(
    "<p>Hi "+name+", welcome to stackoverflow</p>"
  );
});

Then calling {{{welcome "sagittarius"}}} will return

Hi sagittarius, welcome to stackoverflow

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