Created
January 18, 2013 08:32
-
-
Save efazati/4563171 to your computer and use it in GitHub Desktop.
some helper for template in flask
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
from functools import wraps | |
from flask import current_app, render_template, g, jsonify, redirect | |
def title(title=None): | |
def decorator(f): | |
@wraps(f) | |
def decorated_function(*args, **kwargs): | |
g.page_title = title | |
return f(*args, **kwargs) | |
return decorated_function | |
return decorator | |
def base_path(): | |
""" | |
return true path to template => its just fecade | |
""" | |
return current_app.config["TEMPLATE"] | |
def path(addr): | |
""" | |
dar in function address template sakhte mishavad | |
""" | |
if addr.startswith("."): | |
template_full = addr[1:] | |
else: | |
template_full = base_path() + "/" + addr | |
return template_full | |
def render(template, **context): | |
return render_template(path(template), **context) | |
def ajax_render(template, **context): | |
return jsonify(html=render(template, **context)) | |
def ajax_redirect(url): | |
return jsonify(redirect=redirect(url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment