user3210458
user3210458

Reputation: 33

Python Bottle server template returning ill formatted html code

So during the following bottle server route everything works fine...

@route('/IFC/config/<policy>')
def config(policy):
    if policy == "test":
        html_nodes = ""
        Nodes = IFC_main.get_nodes(ipaddr,username,password)
        for node in Nodes:
            html_nodes += '<li>'+node["name"]+'</li>'
        return template("mgmt.tpl",html_nodes = html_nodes)

except when I look at the source code for the webpage that this produces it should be a dropdown menu with the values I provided but instead I get this...

<script language="JavaScript" type="text/javascript" src="/static/mgmt.js">               </script>
<link rel="stylesheet" type="text/css" href="/static/mgmt.css">

<body style = "background-color:#CCCCCA">
<img id="banner" style = "bg-color:CBCCCE" src="static/Cisco_emailHeader.png" alt="Banner Image"/>
<div style="width: 800px;height: 100px;position: absolute;top:0;bottom: 0;left: 0;right: 0;margin: auto;">
<h1>Management Connectivity</h1>
 <ul class="dropdown-menu">
 &lt;li&gt;calo2-leaf3&lt;/li&gt;&lt;li&gt;calo2- spine1&lt;/li&gt;&lt;li&gt;calo2-leaf2&lt;/li&gt;&lt;li&gt;calo2- leaf1&lt;/li&gt;&lt;li&gt;apic2&lt;/li&gt;&lt;li&gt;calo2- spine2&lt;/li&gt;&lt;li&gt;apic1&lt;/li&gt;&lt;li&gt;apic3&lt;/li&gt;
</ul>
</div>
</body>

I understand I need to convert the string I'm passing into the template but I just haven't been able to figure out what. I'm assuming someone else has ran into this issue.

Upvotes: 1

Views: 137

Answers (2)

parthiban
parthiban

Reputation: 143

You can start the statement with an exclamation mark to disable escaping for that statement:

>>> template('Hello {{name}}!', name='<b>World</b>')
u'Hello &lt;b&gt;World&lt;/b&gt;!'
>>> template('Hello {{!name}}!', name='<b>World</b>')
u'Hello <b>World</b>!'

Upvotes: 2

Qiang Jin
Qiang Jin

Reputation: 4467

You need to move the loop into template file,

% for node in nodes:
    <li>{{ node.name }}</li>
% end

The code will change to,

@route('/IFC/config/<policy>')
def config(policy):
    if policy == "test":
        nodes = IFC_main.get_nodes(ipaddr,username,password)
        return template("mgmt.tpl", nodes=nodes)

Upvotes: 1

Related Questions