Created
September 27, 2013 07:20
-
-
Save oschettler/6725179 to your computer and use it in GitHub Desktop.
A simple class to include blocks of code in a template for bottle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Block: | |
''' | |
A simple block class for Bottle templates. | |
Usage: | |
Pass a named block into your template by returning it from an action: | |
return dict(scripts = Block('scripts')) | |
In your template, use it like this: | |
%from bottle import template | |
%scripts.append(template('path/to/sub-template', var=value)) | |
In your layout, include the aggregated scripts: | |
%if defined('scripts'): | |
{{!scripts()}} | |
%end | |
''' | |
name = 'block' | |
text = '' | |
def __init__(self, name): | |
self.name = name | |
self.text = '' | |
def append(self, text): | |
self.text += text | |
def __call__(self): | |
return self.text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment