pythonhtmlbottlemako

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?


Solution

  • 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")}