Python: Django {{ }} tag and layui's laytpl template separator conflict resolution method

When using layui, you need to use the template of the layui data table. At this time, you encounter the problem of { {}} escaping. In django, { {}} is to obtain variable values, which conflicts with layui's laytpl template separator { {}} [{ {}} is the default separator in laytpl] .

Method 1: If the current page does not use the layui table , you can use the redefinition separator

laytpl.config({
  open: '<%',
  close: '%>'
});

//分割符将必须采用上述定义的
laytpl([
  '<%# var type = "公"; %>' //JS 表达式
  ,'<% d.name %>是一位<% type %>猿。'
].join('')).render({
  name: '贤心'
}, function(string){
  console.log(string); //贤心是一位公猿
});   

 

Method 2: If the current page uses layui's table , then django needs to not translate the specified content.

Do not redefine the delimiter, because the delimiter has been written in the source code of the data table component of layui, so the problem cannot be solved by customizing the template delimiter in laytpl.config.

If used, a pair of garbled characters will appear on the page

{
   
   {# if(d.data.toolbar){ }}

{
   
   {# } }}
{
   
   {# if(d.data.loading){ }}{
   
   {# } }}{
   
   {# var left, right; }}
{
   
   {# layui.each(d.data.cols, function(i1, item1){ }}{
   
   {# layui.each(item1, function(i2, item2){ }}{
   
   {# if(item2.fixed && item2.fixed !== "right"){ left = true; } }}{
   
   {# if(item2.fixed === "right"){ right = true; } }}{
   
   {# var isSort = !(item2.colGroup) && item2.sort; }}{
   
   {# }); }}{
   
   {# }); }}
{
   
   {# if(item2.type === "checkbox"){ }}{
   
   {# } else { }}{
   
   {item2.title||""}}{
   
   {# if(isSort){ }}{
   
   {# } }}{
   
   {# } }}

{
   
   {# if(item2.type === "checkbox"){ }}{
   
   {# } else { }}{
   
   {item2.title||""}}{
   
   {# if(isSort){ }}{
   
   {# } }}{
   
   {# } }}

Thanks: https://www.cnblogs.com/ligh-test/p/9838145.html

Since Django 1.5, {% verbatim %} tags are supported (verbatim means literally translated, literally), and Django will not render the content wrapped by the verbatim tag.

So just add {% verbatim %} and {% endverbatim %} tags before and after { {}}

Original error code:

<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{
   
   { d.name }} </a>

The modified executable code:

<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{% verbatim %}{
   
   { d.name }} {% endverbatim %}</a>

 

Guess you like

Origin blog.csdn.net/weixin_38676276/article/details/107924536
Recommended