Mayur Tendulkar
Mayur Tendulkar

Reputation: 81

jsRender Rendering HTML

I'm using jsRender to render my data on html page. The data gets rendered perfectly. However, in some content items, HTML text (like hyperlink) appears as text. In jquery template, there is a way to render HTML by {{html. Is there anything similar available in jsRender? My code is as follows:

<p>
{{ =Status}}   //need to convert this to HTML so it should appear as hyperlink.
</p> 

Thanks.

Upvotes: 5

Views: 11499

Answers (4)

Chintan Mathukiya
Chintan Mathukiya

Reputation: 393

Try this..

<p>
{^{ =Status}}   
</p>

Upvotes: 0

Blaise
Blaise

Reputation: 22212

Use {{:Status}} to generate HTML code.

Upvotes: 6

John Papa
John Papa

Reputation: 22298

The JsRender beta candidate is now out (see Boris' post from last night http://www.borismoore.com/2012/03/approaching-beta-whats-changing-in_06.html)

I wrote a quick fiddle to show how to render HTML here: http://jsfiddle.net/johnpapa/NfUGB/

Basically, just use the {{>yourProperty}} tag to render it HTML encoding. Use {{:yourProperty}} to skip encoding.

<script id="template" type="text/x-jsrender">
    <p>
    {{:foo}}
    </p>
    <ul>
    {{for testData}}
        <li>{{:name}} - {{:markup}} - {{>markup}}</li>
    {{/for}}
    </ul>
</script>
<div id="div1"></div>

.

var vm = {
    foo: "names",
    testData: [
        {
            name: "John", 
            markup: "<span style='background: yellow'>John</span>"
        },
        {
            name: "Boris", 
            markup: "<span style='background: orange'>Boris</span>"
        }
    ]
};

$("#div1").html($("#template").render(vm));​
​

Upvotes: 9

devnull69
devnull69

Reputation: 16544

Looking at jsRender's source code I can see that the plugin converts the <, > and & characters into their HTML entities. Maybe you can try to change those lines from jsRender

escapeMapForHtml = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;"
},

to

escapeMapForHtml = {
},

Mind the security risk of inserting unchecked HTML from external sources!

EDIT: OK, checking the examples (esp. 03_no-encoding.html) brought another solution. So go ahead and undo my previously proposed change and try using

{{=Status!}

The exclamation mark should prevent jsRender from changing HTML

Upvotes: 2

Related Questions