Javascript 更改瓶子中html标记的更好方法

Javascript 更改瓶子中html标记的更好方法,javascript,jquery,html,python-2.7,bottle,Javascript,Jquery,Html,Python 2.7,Bottle,我在跑第12节的瓶子。我有一个函数,它创建我想要的输出(带有html标记),并以字符串形式返回值。我想将该字符串写入html页面,但它会将我的标记替换为'<','>',等等。现在我的解决方案是在页面加载后运行javascript,找到这些模式并替换它们。有更好的办法吗 范例 函数。py: def create_html(dict_in): output ='<li class="myclass">' output +='<div class="class

我在跑第12节的瓶子。我有一个函数,它创建我想要的输出(带有html标记),并以字符串形式返回值。我想将该字符串写入html页面,但它会将我的标记替换为'<','>',等等。现在我的解决方案是在页面加载后运行javascript,找到这些模式并替换它们。有更好的办法吗

范例

函数。py:

def create_html(dict_in):
    output ='<li class="myclass">'
    output +='<div class="class_link">'
    output +='<a href="/showDevice?id='+str(dict_in['_id'])+'">'+dict_in['name']+'&nbsp;</a>'

    return output
%import lib.functions
<div id="content">
    <div class="device_wrapper">
        %for thing in list_of_things:
            {{lib.functions.create_html(thing)}}
        %end #end for loop
    </div>
</div>
def create_html(dict_in):
输出='
  • ' 输出+='' 输出+='' 返回输出
  • 模板。第三方物流:

    def create_html(dict_in):
        output ='<li class="myclass">'
        output +='<div class="class_link">'
        output +='<a href="/showDevice?id='+str(dict_in['_id'])+'">'+dict_in['name']+'&nbsp;</a>'
    
        return output
    
    %import lib.functions
    <div id="content">
        <div class="device_wrapper">
            %for thing in list_of_things:
                {{lib.functions.create_html(thing)}}
            %end #end for loop
        </div>
    </div>
    
    %import lib.functions
    %对于物品列表中的物品:
    {{lib.functions.create_html(thing)}
    %结束#循环结束
    
    custom.js

    $('#change_tags').each(function(){
        var $this = $(this);
        var t = $this.text();
        $this.html(t.replace('\"',''));
        $this.html(t.replace('&lt','<').replace('&gt', '>'));
    
    });$
    
    $('#更改标签')。每个(函数(){
    var$this=$(this);
    var t=$this.text();
    $this.html(t.replace('\','');
    $this.html(t.replace('<','');
    });$
    
    tldr;如何在瓶子更改标签之前更改标签?是否有非js解决方案?

    根据,您可以通过在表达式前添加感叹号来关闭转义html:

    %import lib.functions
    <div id="content">
        <div class="device_wrapper">
            %for thing in list_of_things:
                {{!lib.functions.create_html(thing)}}
            %end #end for loop
        </div>
    </div>
    
    %import lib.functions
    %对于物品列表中的物品:
    {{!lib.functions.create_html(thing)}
    %结束#循环结束
    
    这篇文章我一定读过很多遍了,但从未看过。谢谢!