簡體   English   中英

python龍卷風使用mako模板,如何使用static_url方法?

[英]python tornado use mako template, how to use static_url method?

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import _env
import tornado.web
import mako.lookup
import mako.template
from os.path import join


TEMPLATE_PATH = [join(_env.PREFIX, 'templates')]

MAKO_LOOK_UP = mako.lookup.TemplateLookup(
    directories=TEMPLATE_PATH,
    input_encoding='utf-8',
    output_encoding='utf-8',
    filesystem_checks=False,
    encoding_errors='replace',
    module_directory=join(_env.PREFIX, '_templates'),
)


class BaseHandler(tornado.web.RequestHandler):
    def initialize(self, lookup=MAKO_LOOK_UP):
        '''Set template lookup object, Defalut is MAKO_LOOK_UP'''
        self._lookup = lookup

    def render_string(self, filename, **kwargs):
        '''Override render_string to use mako template.
        Like tornado render_string method, this method
        also pass request handler environment to template engine.
        '''
        try:
            template = self._lookup.get_template(filename)
            env_kwargs = dict(
                handler=self,
                request=self.request,
                current_user=self.current_user,
                locale=self.locale,
                _=self.locale.translate,
                static_url=self.static_url,
                xsrf_form_html=self.xsrf_form_html,
                reverse_url=self.application.reverse_url,
            )
            env_kwargs.update(kwargs)
            return template.render(**env_kwargs)
        except:
            # exception handler
            pass

    def render(self, filename, **kwargs):
        self.finish(self.render_string(filename, **kwargs))

大家好。 我是龍卷風和mako的新手。 這段代碼我將龍卷風默認模板更改為mako,我知道在龍卷風中我可以通過以下方式在html中使用static_url: <link rel="stylesheet" href="{{ static_url("css/reset.css") }}">但是如何在mako html中使用它? 我嘗試了一些不同的方法,但是沒有用。 任何人都可以幫助,謝謝。

<link rel="stylesheet" href="${static_url("css/reset.css") } ">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM