Skip to content

Instantly share code, notes, and snippets.

@efazati
Created January 18, 2013 08:32
Show Gist options
  • Save efazati/4563171 to your computer and use it in GitHub Desktop.
Save efazati/4563171 to your computer and use it in GitHub Desktop.
some helper for template in flask
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