示例#1
0
文件: mako.py 项目: podhmo/korpokkur
def _render_context(tmpl, callable_, context, args, kwargs):
    import mako.template as template
    # create polymorphic 'self' namespace for this
    # template with possibly updated context
    if not isinstance(tmpl, template.DefTemplate):
        # if main render method, call from the base of the inheritance stack
        (inherit, lclcontext) = mako_runtime._populate_self_namespace(context, tmpl)
        mako_runtime._exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
    else:
        # otherwise, call the actual rendering method specified
        (inherit, lclcontext) = mako_runtime._populate_self_namespace(context, tmpl.parent)
        mako_runtime._exec_template(callable_, context, args=args, kwargs=kwargs)
示例#2
0
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 ''
示例#3
0
文件: template.py 项目: fordream/me
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 ''
示例#4
0
def create_mako_context(template_obj, **kwargs):
    # I'm hacking into private Mako methods here, but I can't see another
    # way to do this.  Hopefully this can be rectified at some point.
    kwargs.pop(
        'self', None
    )  # some contexts have self in them, and it messes up render_unicode below because we get two selfs
    runtime_context = MakoContext(io.StringIO(), **kwargs)
    runtime_context._set_with_template(template_obj)
    _, mako_context = _populate_self_namespace(runtime_context, template_obj)
    return mako_context
示例#5
0
def _render_context(tmpl, callable_, context, args, kwargs):
    import mako.template as template
    # create polymorphic 'self' namespace for this
    # template with possibly updated context
    if not isinstance(tmpl, template.DefTemplate):
        # if main render method, call from the base of the inheritance stack
        (inherit,
         lclcontext) = mako_runtime._populate_self_namespace(context, tmpl)
        mako_runtime._exec_template(inherit,
                                    lclcontext,
                                    args=args,
                                    kwargs=kwargs)
    else:
        # otherwise, call the actual rendering method specified
        (inherit, lclcontext) = mako_runtime._populate_self_namespace(
            context, tmpl.parent)
        mako_runtime._exec_template(callable_,
                                    context,
                                    args=args,
                                    kwargs=kwargs)
示例#6
0
文件: view.py 项目: tru-software/tru
 def load_template(self):
     if not self.__mako_template or settings.MAKO_ALWAYS_RELOAD:
         if not self.__mako_lookup:
             self.__build()
         # To załaduje sam plik mako
         try:
             self.__mako_template = self.__mako_lookup.get_template(
                 self.__mako_filename)
             # natomiast trzeba jeszcze załadować nadrzędne mako
             tmp_context = mako_runtime.Context(
                 mako_util.FastEncodingBuffer())
             tmp_context._with_template = self.__mako_template
             mako_runtime._populate_self_namespace(tmp_context,
                                                   self.__mako_template)
         except Exception as ex:
             log.error("Cannot load template: %s (%s) in %s: %s" %
                       (self.__mako_filename, self.__mako_template.module
                        if self.__mako_template else 'file not found',
                        self.__mako_lookup.directories, ex))
             # print "Cannot load template: %s (%s)" % (self.__mako_filename, self.__mako_template.module if self.__mako_template else 'file not found')
             raise