I can not render html in python 3 [closed]

1

I have the following app:

#!/usr/bin/python3

from bottle import template, route, run
import html
cabeca = ('''
<html lang="pt-br">
    <head>
    </head>
    <body>

        <nav id="menu">
            {{menu}}
        </nav>

        <main id="conteudo">
            {{conteudo}}
        </main>

    </body>
</html>
''')
menu = (('add', 'flaticon-add', 'Adicionar'),
('busca', 'flaticon-target', 'Buscar'),
('balanco', 'flaticon-coin', 'Balanço'),
('conf', 'flaticon-settings', 'Configurações'))

def cria_menu():
    bloco = ('<nav id="menu">')
    for item in menu:
        bloco += ('<div class="menu-item">\
        <a class="menu-link" href="{}">\
        <img class="{} menu-icon" alt="{}"/>\
        </a>\
        </div>\
        '.format(item[0], item[1], item[2]))
    bloco += ('</nav>')
    return(bloco)

@route('/')
def index():
    return(template(cabeca, menu = html.unescape(cria_menu())\
    .replace('&lt;', '<')\
    .replace('&gt;', '>')\
    .replace('&quot;', '"'), conteudo = 'Alguma coisa'))

run(port = 8000, debug = True, reloader = True)

No matter what I do, I can not render the page.

    
asked by anonymous 29.08.2018 / 07:14

1 answer

0

The problem is that the {{ variavel }} syntax exists to include common text in your HTML, not to include more HTML code. This syntax causes the bottle to automatically "escape" the entire contents of the variable so that it appears in the result as it is, and prevent XSS attacks if the variable comes from the user.

To include a template in the other, the correct would be to use include() or rebase() as in example documentation .

But for a quick test, you can include the exclamation ! before the variable name, which forces the bottle not to "escape" the content and leave it as-is:

    <nav id="menu">
        {{!menu}}
    </nav>

    <main id="conteudo">
        {{!conteudo}}
    </main>
    
29.08.2018 / 20:13