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.
source to share
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("<").split(">").join(">").split('"').join(""").split("'").join("'");
}
to
encode: function( text ) {
// Do HTML encoding replacing < > & and ' and " by corresponding entities.
return text;
}
source to share