Skip to content

Instantly share code, notes, and snippets.

@justinfay
Last active May 23, 2016 14:26
Show Gist options
  • Select an option

  • Save justinfay/a0c4a23a960770c3296e to your computer and use it in GitHub Desktop.

Select an option

Save justinfay/a0c4a23a960770c3296e to your computer and use it in GitHub Desktop.
# Idea shamelessly stolen from pyramid.
# utils.py
from collections import Mapping
from functools import wraps
from flask import has_request_context, render_template
def render_dict_template(template):
def decorator(func):
@wraps(func)
def _decorator(*args, **kwargs):
result = func(*args, **kwargs)
# Allow for other return values such as redirect.
if isinstance(result, Mapping) and has_request_context():
return render_template(template, **result)
return result
return _decorator
return decorator
# views.py
from subshop import shop, storage, utils
@shop.route('/')
@shop.route('/plans')
@utils.render_dict_template('plan_list.html')
def plan_list():
return {'plans': storage.get_plans()}
# In terminal
>>> from subshop import views
>>> assert views.plan_list() == {'plans': [...]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment