Search code examples
jqueryjquery-pluginsjquery-templates

htmlentities equivalent in jQuery template plugin


How can I convert special characters in a string to its equivalent htmlentities in jQuery template plugin? I know about the $('<div/>').text(str).html() method in jQuery. I am looking for a way to implement it in the template plugin.


Solution

  • Is this what you want?

    js code:

    var data = { HtmlContent : '<h1>The h1 tag should not display</h>' };
    
    $('#tmplSample').tmpl(data).appendTo('body');
    

    template:

    <script id="tmplSample" type="text/x-jquery-tmpl">
    <div>
    {{html HtmlContent}}
    </div>
    </script>
    

    And here are some more samples:

    http://api.jquery.com/template-tag-html/