简体   繁体   中英

Use templates in Python Bottle

Currently, I use Python Bottle in one of my projects. I want to change it and use templates like Jinja2 tutorials with extends and includes to avoid repeating navbars, footers etc.

In Jinja2 I could use something like this:

{% extends base.html %}

{% block maincontent %}
<here my HTML code>
{% endblock %}

I found that I can use this with Bottle by importing Jinja2 view and template, but then I have to reformat every Python code in HTML pages from Bottle to Jinja2 formats.

For example:

from:

%for i in mylist:
<option>{{i}}</option>
%end

to:

{% for i in mylist %}
<option>{{i}}</option>
{% endfor %}

Is there any way to use extends with Bottle template without changing all my Python code? I cannot find any tutorial.

The template engine bottle uses, SimpleTemplate, doesn't support inheritance. You can, however, use include to separate things like headers and footers, or rebase to simulate inheritance.

Of course, since SimpleTemplate is, well, simple , if you know you'll need more advanced templating features, it'll be less costly to migrate now rather than later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM