def render(template_, **kwargs): session = request.get_cookie('t') user = Account.get_user_from_session_id(session) kwargs['user'] = user templ = MakoTemplate(name=template_, lookup=[TEMPLATES_PATH], imports=['from webhelpers.html import escape'], default_filters=['escape']) return templ.render(**kwargs)
def test_inherit(self): """ Templates: Mako lookup and inherience """ t = MakoTemplate(name='mako_inherit', lookup=['./views/']).render(var='v') self.assertEqual('o\ncvc\no\n', ''.join(t)) t = MakoTemplate('<%inherit file="mako_base.tpl"/>\nc${var}c\n', lookup=['./views/']).render(var='v') self.assertEqual('o\ncvc\no\n', ''.join(t)) t = MakoTemplate('<%inherit file="views/mako_base.tpl"/>\nc${var}c\n', lookup=['./']).render(var='v') self.assertEqual('o\ncvc\no\n', ''.join(t))
def template(name='', data={}): response.content_type = 'text/html; charset=utf-8' return MakoTemplate(name=name, lookup=['./templates/' ]).render(data, default_filters=['decode.utf8'], output_encoding='utf-8')
def render_template(name, error = False, **kwargs): active_language = request.headers.get('Active-Language') target_name = get_final_path(name, start_w_slash = False) template = MakoTemplate(name=target_name, lookup=[TEMPLATES_PATH], default_filters=['decode.utf8'], imports=['from episte_web.libs.templates import get_final_path']) search_classification = request.GET.get('classification', 'all') # if error: # try: # get_account(request.mongodb) # except: # request.account = None # else: # get_account(request.mongodb) kwargs['messages'] = get_messages(kwargs.get('messages')) for key in kwargs: kwargs[key] = html_escape(kwargs[key]) return template.render(language = active_language, languages = LANGUAGES, search_classification = search_classification, **kwargs)
def test_string(self): """ Templates: Mako string""" t = MakoTemplate('start ${var} end').render(var='var') self.assertEqual('start var end', t)
def test_name(self): """ Templates: Mako lookup by name """ with chdir(__file__): t = MakoTemplate(name='mako_simple', lookup=['./views/']).render(var='var') self.assertEqual('start var end\n', t)
# -*- coding: utf-8 -*- from markupsafe import escape, Markup from wellpapp import Client, Config, DotDict from urllib import urlencode import re from math import ceil from bottle import request, response, MakoTemplate MakoTemplate.global_config("imports", ["from markupsafe import escape"]) MakoTemplate.global_config("default_filters", ["escape"]) def init(): request.outdata_head = [] request.outdata = [] request.client = Client(cfg) return request.client cfg = Config(local_rc=True) per_page = 32 user = "******" base = unicode(cfg.webbase) assert base thumbsize = unicode(cfg.thumb_sizes.split()[0]) assert thumbsize def tag_clean(n): """Get tagname without prefix""" if n[0] in u"-~!": return n[1:] return n
def test_file(self): """ Templates: Mako file""" t = MakoTemplate(name='./views/mako_simple.tpl').render(var='var') self.assertEqual('start var end\n', t)
def test_name(self): """ Templates: Mako lookup by name """ t = MakoTemplate(name='mako_simple', lookup=['./views/']).render(var='var') self.assertEqual('start var end\n', ''.join(t))
# active_language = 'en' # Reinstalling the translation. This should be done by the middleware, but maybe is not working well. # translation = gettext.translation( # LOCALE_NAME, # LOCALE_PATH, # languages=[active_language], # codeset="utf-8" # ) # translation.install(unicode=True) target_name = get_final_path(name, '', start_w_slash=False) template = MakoTemplate( name=target_name, lookup=[TEMPLATES_PATH], default_filters=['decode.utf8'], imports=['from plants_web.libs.templates import get_final_path'], cache_enabled=False) search_classification = request.GET.get('classification', 'all') # if error: # try: # get_account(request.mongodb) # except: # request.account = None # else: # get_account(request.mongodb) # kwargs['messages'] = get_messages(kwargs.get('messages')) # for key in kwargs: # kwargs[key] = html_escape(kwargs[key]) try:
# Reinstalling the translation. This should be done by the middleware, but maybe is not working well. # translation = gettext.translation( # LOCALE_NAME, # LOCALE_PATH, # languages=[active_language], # codeset="utf-8" # ) # translation.install(unicode=True) target_name = get_final_path(name, "", start_w_slash=False) template = MakoTemplate( name=target_name, lookup=[TEMPLATES_PATH], default_filters=["decode.utf8"], imports=["from plants_web.libs.templates import get_final_path"], cache_enabled=False, ) search_classification = request.GET.get("classification", "all") # if error: # try: # get_account(request.mongodb) # except: # request.account = None # else: # get_account(request.mongodb) # kwargs['messages'] = get_messages(kwargs.get('messages')) # for key in kwargs: # kwargs[key] = html_escape(kwargs[key])
from bottle import Bottle, MakoTemplate, route simple_app = app = Bottle() hello_template = MakoTemplate('<b>Hello ${name}</b>!') bye_template = MakoTemplate('<b>Bye ${name}</b>!') @app.route('/hello/<name>') def index(name): return hello_template.render(name=name) @app.route('/bye/<name>') def index(name): return bye_template.render(name=name)