javascript - Rendering content with HTML tags in Ractive.js - Stack Overflow

I'm trying to include HTML tags in data rendered by Ractive.js (to auto-subscript chemical symbols

I'm trying to include HTML tags in data rendered by Ractive.js (to auto-subscript chemical symbols, in case anyone cares), but Ractive just inserts the text rather than creating new DOM elements.

A minimal test case, adapted from the 60-second setup guide:

<script id='template' type='text/ractive'>
    <p>Hello, {{name}}!</p>
</script>

<script type="text/javscript">
    var ractive = new Ractive({
    el: 'container',

    template: '#template',

    data: { name: '<b>world</b>' }
});

As you can see in this JSfiddle, the result is Hello, <b>world</b> rather than the expected "Hello, world"

I'm guessing this is to do with the way Ractive handles expressions.. but I'm not yet at the point where the source makes much sense.

Any thoughts on how I could get the intended behaviour?

I'm trying to include HTML tags in data rendered by Ractive.js (to auto-subscript chemical symbols, in case anyone cares), but Ractive just inserts the text rather than creating new DOM elements.

A minimal test case, adapted from the 60-second setup guide:

<script id='template' type='text/ractive'>
    <p>Hello, {{name}}!</p>
</script>

<script type="text/javscript">
    var ractive = new Ractive({
    el: 'container',

    template: '#template',

    data: { name: '<b>world</b>' }
});

As you can see in this JSfiddle, the result is Hello, <b>world</b> rather than the expected "Hello, world"

I'm guessing this is to do with the way Ractive handles expressions.. but I'm not yet at the point where the source makes much sense.

Any thoughts on how I could get the intended behaviour?

Share Improve this question asked May 7, 2014 at 10:14 supervacuosupervacuo 9,2622 gold badges46 silver badges61 bronze badges 1
  • The JSFiddle does not render as expected for these browsers tried today. Why? Microsoft Edge 44.17754.1.0 and Google Chrome Version 69.0.3497.100 – user2063329 Commented Oct 1, 2018 at 3:45
Add a ment  | 

1 Answer 1

Reset to default 11

Yes! You can use the triple-stache:

<p>Hello, {{{name}}}!</p>

In traditional Mustache, this means 'don't escape the value of name'. Translated to Ractive, it means 'insert the contents as HTML rather than text.

Be aware that the HTML is not sanitised before insertion, so if name came from user input there's potential for XSS and other nasties: http://jsfiddle/rich_harris/4jBC8/.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744117930a4559261.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信