jsRender Rendering HTML

空扰寡人 提交于 2019-12-05 10:36:47
|improve this question

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));​
​

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

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

Try this..

<p>
{^{ =Status}}   
</p>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!