Search code examples
javascripthandlebars.jshtml-escape

Adding "<strong>" to a certain word using handlebars.js


I am using handlebars for JavaScript templating. I want to add around the word "success". I would hopefully want to escape any other html or scripts in the text.


Solution

  • You're problem isn't very descriptive, but this is what I think you're after:

    Handlebars.registerHelper('strongSuccess', function(str) {
        str = Handlebars.Utils.escapeExpression(str);
        str = str.replace('success','<strong>success</strong>');
        return new Handlebars.safeString(str);
    });
    

    It will first escape all of the initial HTML. After that, you add in your own unescaped <strong> element, and then return a safeString. Good luck :)