如何在laravel 4.2中使用handlebar js转义html

How to escaping html in laravel 4.2 with handlebar js?

本文关键字:handlebar js 转义 html laravel      更新时间:2023-09-26

这是我在laravel blade中的html代码。

<script id="expressions-template" type="text/x-handlebars-template">
   @{{description.escaped}}
   @{{example}}
   @{{description.unescaped}}
   @{{{example}}}
</script>
<div class="content-placeholder"></div>
这是我的js脚本编译模板与数据
$(function () {
 // Grab the template script
 var theTemplateScript = $("#expressions-template").html();
 // Compile the template
 var theTemplate = Handlebars.compile(theTemplateScript);
 // Define our data object
 var context={
   "description": {
      "escaped": "Using {{}} brackets will result in escaped HTML:",
      "unescaped": "Using {{{}}} will leave the context as it is:"
      },
   "example": "<button> Hello </button>"
   };
 // Pass our data to the template
 var theCompiledHtml = theTemplate(context);
 // Add the compiled html to the page
  $('.content-placeholder').html(theCompiledHtml);
 });

laravel (4.2) blade不转义HTML标签,它只是将HTML标签作为文本打印

Blade::setEscapedContentTags('[[', ']]');
Blade::setContentTags('[[[', ']]]');

为一个视图使用不同的标签,你可以在闭包或控制器动作中设置这些标签,它们将生成视图。

车把模板使用花括号