Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webhelpers with Python Bottle

I've got a Python Bottle application up and running use Mako templating. I'd like to use the webhelpers module (used in Pylons web applications) but I'm not sure how to go about doing this. I'd like to be able to do this in my mako templates:

${h.stylesheet_link("some_link_url_text")}

and have it produce the correct link HTML. But when I run this, h is undefined, as you might imagine.

How can I do this?

like image 806
writes_on Avatar asked Dec 10 '25 01:12

writes_on


1 Answers

Webhelpers is a seperate module which can be installed easily.

pip install webhelpers

Then you can create a python moduler called helpers.py. In the module then import all the functions you want available like below

"""
helpers.py
Import all webhelpers that you want to have access to
"""
from webhelpers.html.tags import stylesheet_link

Then in your bottle app import the helpers module and then pass it to your template as h.

import helpers

@route('/')
def index():
    return Template("template_name").render(h=helpers)

Then in your templete use it like you have above

${h.stylesheet_link("some_link_url_text")}
like image 80
i_4_got Avatar answered Dec 12 '25 15:12

i_4_got



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!