def _render_tmpl_func(tmpl, func, **kwargs): # 先尝试寻找本template中的func func = str(func) if tmpl.has_def(func): _t = tmpl.get_def(func) else: # 在当前template的parent中寻找该func from mako import util from mako.template import DefTemplate from mako.runtime import Context, _populate_self_namespace # disable unicode 状态下生成 context buf = util.StringIO() context = Context(buf, **kwargs) context._with_template = tmpl # 将当前的namespace, 如'self', 'next'等放入context func_render_body, context = _populate_self_namespace(context, tmpl) # 找到当前template的inherits self_ns = context['self'] inherit_m = self_ns.inherits.module # 在inherits中寻找该func // mako 模板中为render_xxx func_name = "render_%s" % func if hasattr(inherit_m, func_name): _t = DefTemplate(tmpl, getattr(inherit_m, func_name)) else: _t = None if 'self' in kwargs: kwargs.pop('self') return _t and _t.render(**kwargs) or ''
def _render_tmpl_func(tmpl, func, **kwargs): func = str(func) if tmpl.has_def(func): _t = tmpl.get_def(func) else: from mako import util from mako.template import DefTemplate from mako.runtime import Context, _populate_self_namespace buf = util.StringIO() context = Context(buf, **kwargs) context._with_template = tmpl func_render_body, context = _populate_self_namespace(context, tmpl) self_ns = context['self'] inherit_m = self_ns.inherits.module func_name = "render_%s" % func if hasattr(inherit_m, func_name): _t = DefTemplate(tmpl, getattr(inherit_m, func_name)) else: _t = None if 'self' in kwargs: kwargs.pop('self') return _t and _t.render(**kwargs) or ''