def render_mako(template=None, info=None, format=None, fragment=False, mapping=None): global mako, mako_lookup if mako is None: import mako import mako.lookup mako_lookup = mako.lookup.TemplateLookup(directories=['']) extension = format if "." in template: module, filename = template.rsplit(".", 1) filename = '%s.%s' % (filename, extension) template = resource_filename(module, filename) else: template = '%s.%s' % (template, extension) templ = mako_lookup.get_template(template) try: ret = templ.render(**info) except Exception: ret = mako.exceptions.html_error_template().render() return ret
def render_kid(template=None, info=None, format=None, fragment=False, mapping=None): """We need kid support in order to get some of the tests working """ global kid if kid is None: import kid extension = "kid" if "." in template: module, filename = template.rsplit(".", 1) filename = '%s.%s' % (filename, extension) template = resource_filename(module, filename) else: template = '%s.%s' % (template, extension) template = kid.Template(file=template, fragment=fragment, **info) return template.serialize()
def render_genshi(template=None, info=None, format=None, fragment=False, mapping=None): global genshi, genshi_loader if genshi is None: # Lazy imports of Genshi import genshi import genshi.template import genshi.output import genshi.input import genshi.filters def genshi_loader_callback(template): """This function will be called by genshi TemplateLoader after loading the template""" translator = genshi.filters.Translator(gettext) # Genshi 0.6 supports translation directives. Lets use them if available. if hasattr(translator, "setup"): translator.setup(template) else: template.filters.insert(0, translator) if config.get("i18n.run_template_filter", False): callback = genshi_loader_callback else: callback = None auto_reload = config.get("genshi.auto_reload", "1") if isinstance(auto_reload, basestring): auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') max_cache_size = config.get("genshi.max_cache_size", 25) genshi_loader = genshi.template.TemplateLoader( [""], auto_reload=auto_reload, callback=genshi_loader_callback, max_cache_size=max_cache_size, ) # Choose Genshi template engine if format == "text": cls = genshi.template.NewTextTemplate default_extension = "txt" else: cls = None # Default is Markup default_extension = "html" if "/" in template: # Path notation. Use first path part as module name module, pathname = template.split("/", 1) template = resource_filename(module, pathname) elif "." in template: # Dotted notation module, filename = template.rsplit(".", 1) filename = '%s.%s' % (filename, default_extension) template = resource_filename(module, filename) else: template = '%s.%s' % (template, default_extension) encoding = config.get("genshi.encoding", "utf-8") templ = genshi_loader.load(template, encoding=encoding, cls=cls) if format == 'html' and not fragment: mapping.setdefault( 'doctype', config.get('genshi.default_doctype', 'html-transitional')) serializer = genshi.output.get_serializer(format, **mapping) extras = { 'XML': genshi.input.XML, 'HTML': genshi.input.HTML, 'ET': genshi.input.ET, '_': lazy_gettext } context = genshi.template.Context(**extras) context.push(stdvars()) context.push(info) stream = templ.generate(context) if config.get('genshi.html_form_filler', False): stream = stream | genshi.filters.HTMLFormFiller(data=info) encode = genshi.output.encode return encode(serializer(stream), method=serializer, encoding=encoding)
def render_genshi(template=None, info=None, format=None, fragment=False, mapping=None): global genshi, genshi_loader if genshi is None: # Lazy imports of Genshi import genshi import genshi.template import genshi.output import genshi.input import genshi.filters def genshi_loader_callback(template): """This function will be called by genshi TemplateLoader after loading the template""" translator = genshi.filters.Translator(gettext) # Genshi 0.6 supports translation directives. Lets use them if available. if hasattr(translator, "setup"): translator.setup(template) else: template.filters.insert(0, translator) if config.get("i18n.run_template_filter", False): callback = genshi_loader_callback else: callback = None auto_reload = config.get("genshi.auto_reload", "1") if isinstance(auto_reload, basestring): auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') max_cache_size = config.get("genshi.max_cache_size", 25) genshi_loader = genshi.template.TemplateLoader([""], auto_reload=auto_reload, callback=genshi_loader_callback, max_cache_size=max_cache_size, ) # Choose Genshi template engine if format == "text": cls = genshi.template.NewTextTemplate default_extension = "txt" else: cls = None # Default is Markup default_extension = "html" if "/" in template: # Path notation. Use first path part as module name module, pathname = template.split("/", 1) template = resource_filename(module, pathname) elif "." in template: # Dotted notation module, filename = template.rsplit(".", 1) filename = '%s.%s' % (filename, default_extension) template = resource_filename(module, filename) else: template = '%s.%s' % (template, default_extension) encoding = config.get("genshi.encoding", "utf-8") templ = genshi_loader.load(template, encoding=encoding, cls=cls) if format == 'html' and not fragment: mapping.setdefault('doctype', config.get('genshi.default_doctype', 'html-transitional')) serializer = genshi.output.get_serializer(format, **mapping) extras = { 'XML' : genshi.input.XML, 'HTML' : genshi.input.HTML, 'ET' : genshi.input.ET, '_' : lazy_gettext } context = genshi.template.Context(**extras) context.push(stdvars()) context.push(info) stream = templ.generate(context) if config.get('genshi.html_form_filler', False): stream = stream | genshi.filters.HTMLFormFiller(data=info) encode = genshi.output.encode return encode(serializer(stream), method=serializer, encoding=encoding)