Search code examples
javascriptjqueryhtmlpython-2.7bottle

Better way to change html tags in bottle


I am running bottle v .12. I have a function that creates output that I want (with html tags) and returns the value as a string. I want to write that string to an html page, but it replaces my tags with '&lt', '&gt', etc. Right now my solution is to run javascript after the page loads, find those patterns and replaces them. Is there a better way?

example

functions.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

template.tpl:

%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>

custom.js

$('#change_tags').each(function(){
    var $this = $(this);
    var t = $this.text();
    $this.html(t.replace('\"',''));
    $this.html(t.replace('&lt','<').replace('&gt', '>'));

});$

tldr; How do I change the tags before bottle changes them? Is there a non-js solution to this?


Solution

  • According to the bottle documentation, you can turn off escaping html by putting an exclamation mark before the expression:

    %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>