Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 encoding the HTML. When I just output the variable like so:

 ${description}

it results in the raw HTML string being outputted with tags and all like so:

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

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

   {{html $description}}

doesn't output anything!

I tried a zillion ways of passing in the variable, but I think there's something going on in the html routine I'm not getting.

Thanks.

Update:

Here's how it's being 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 $description and the other variables needed.

Another update:

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

does behave as expected. But

 {{html $another_variable}}

fails to produce any output even when $another_variable doesn't have an HTML in it.

Another update:

I also tried:

 ${$item.html(description)}

and changed the call to the template 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 pass through the string unchanged as was recommended:

 html: function(str) {
        return str;
    }

but still no dice.

like image 499
TAH Avatar asked Feb 07 '13 01:02

TAH


1 Answers

The answer is:

{{html description}}

I tried that early on, but because I left stray spaces inside the curly brackets, it did not interpret properly.

:-(

like image 181
TAH Avatar answered Oct 16 '22 23:10

TAH