Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bottle templating

Tags:

python

bottle

I recently got into bottlepy and this is pretty much my first experience with a template engine.(Prior I would simply make what I needed in traditional PHP)

My question is this, let's say I have a base layout(A simple HTML/CSS layout), and, for example; a login box on the side. How do I keep the login box dynamically up to date without having to pass values to it on every route?

Take this for example:

from bottle import route,template

@route('/')
def main():

    return template("content.tpl") #rebase from layout.tpl

layout.tpl:

<html>
    <head>
        <title>{{title}}</title>
    </head>
    <body>
        %include
        <div id='sidebar'><!-- login box --></div>
    </body>
</html>

I guess my main question here is how do I make sure the login box runs without having to insert the variables for it at every single page

like image 884
Idan Elhalwani Avatar asked Sep 10 '25 23:09

Idan Elhalwani


1 Answers

To make a variable available in all templates, put something like this before your routes:

from bottle import BaseTemplate

BaseTemplate.defaults['symbol_name'] = value

I use this to add helper functions for my templates.

A more correct solution may involve writing a plugin, which is not terribly difficult and is covered in the Bottle documentation.

Source

like image 149
nkorth Avatar answered Sep 13 '25 11:09

nkorth