Skip to content

Instantly share code, notes, and snippets.

@alex-ac
Created August 13, 2014 11:09
Show Gist options
  • Save alex-ac/7cecbfc30a8cac3f798a to your computer and use it in GitHub Desktop.
Save alex-ac/7cecbfc30a8cac3f798a to your computer and use it in GitHub Desktop.
Python Bem templator with flask example.
#!/usr/bin/env python3
from flask import Flask
from bem import Bem
import templates
app = Flask(__name__)
bem = Bem(no_cache = True)
bem.load_default_functions()
bem.load_default_level()
@app.route('/css/<name>.css')
def css(name):
return bem.get_css(name)
@app.route('/js/<name>.js')
def js(name):
return bem.get_js(name)
@app.route('/')
@bem.template('index')
@templates.page('index')
def root():
return ('My test page', {'block': 'header'})
if __name__ == '__main__':
app.run(port = 8000, debug = True)
<!doctype html><html class="page"><head><title>My test page</title><meta charset="utf-8"></meta><link href="/css/index.css" rel="stylesheet" type="text/css"></link></head><body class="page__body"><div class="header"></div><script src="/js/index.js" type="text/javascript"></script></body></html>
/* CSS generated by Python-bem */
/* Block header */
/* End of block header */
/* Block page */
/* End of block page */
/* End of CSS generated by Python-bem */
/* JS generated by Python-bem */
/* Block header */
/* End of block header */
/* Block page */
/* End of block page */
/* End of JS generated by Python-bem */
#!/usr/bin/env python3
from flask import url_for
def page(name):
def decorator(function):
def wrapper(*args, **kwargs):
content = function(*args, **kwargs)
output = {
'block': 'page',
'title': content[0],
'stylesheet': url_for('css', name = name),
'script': url_for('js', name = name),
'content': content[1]
}
return output
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment