Can't run html on text string in JQuery tmpl

In a JQuery template in my project, I am trying to write out a variable which is an HTML string without HTML encoding. When I just issue a variable like this:

 ${description}

      

this results in the original HTML string being output with tags and so on:

<p>text <b>bold</b>  <i>italic</i></p>

      

But when I try to turn the markup into real tags at the same place in the code:

   {{html $description}}

      

outputs nothing!

I have tried millions of ways to pass in a variable, but I think something is happening in the html subroutine that I am not getting.

Thank.

Update:

Here's what it's called:

    this.$el.attr('data-id', tmplData.id).
          data('id', tmplData.id).html($.tmpl(this.template, tmplData));

      

Where this.template is the template file and tmplData is JSON, which includes the $ description and other required variables.

Another update:

 {{html '<p>string</p>'}}

      

behaves as expected. But

 {{html $another_variable}}

      

can't produce any output even if $ another_variable doesn't have HTML in it.

Another update:

I've also tried:

 ${$item.html(description)}

      

and changed the template call to

 this.$el.attr('data-id', tmplData.id).
         data('id', tmplData.id).
         html($.tmpl(this.template, tmplData, {
            truncate: _.truncateStrip,
            html: _.html,
        }));

      

And wrote a custom html routine to go through the line unchanged as recommended:

 html: function(str) {
        return str;
    }

      

but still no cubes.

+3


source to share


2 answers


Answer:

{{html description}}

      



I tried this early on, but because I left white spaces inside the curly braces, it was misinterpreted.

: - (

+11


source


If you open source you can change the line that encodes. I haven't seen an option to disable it, so this might be the easiest solution for you.

Edit:

encode: function( text ) {
            // Do HTML encoding replacing < > & and ' and " by corresponding entities.
            return ("" + text).split("<").join("&lt;").split(">").join("&gt;").split('"').join("&#34;").split("'").join("&#39;");
        }

      



to

encode: function( text ) {
            // Do HTML encoding replacing < > & and ' and " by corresponding entities.
            return text;        
}

      

0


source







All Articles