tornado 下劃線括弧是什麼意思?
在tornado源碼的demo中,blog程序的模板有許多單下劃線+括弧的寫法,請問是什麼意思?比如base.html中的 &{{ _("New post") }}&
給你看一段tornado的源碼 希望你能明白 。
def get_template_namespace(self):
"""Returns a dictionary to be used as the default template namespace.
May be overridden by subclasses to add or modify values.
The results of this method will be combined with additional
defaults in the `tornado.template` module and keyword arguments
to `render` or `render_string`.
"""
namespace = 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.reverse_url
)
namespace.update(self.ui)
return namespace
def render_string(self, template_name, **kwargs):
"""Generate the given template with the given arguments.
We return the generated byte string (in utf8). To generate and
write a template as a response, use render() above.
"""
# If no template_path is specified, use the path of the calling file
template_path = self.get_template_path()
if not template_path:
frame = sys._getframe(0)
web_file = frame.f_code.co_filename
while frame.f_code.co_filename == web_file:
frame = frame.f_back
template_path = os.path.dirname(frame.f_code.co_filename)
with RequestHandler._template_loader_lock:
if template_path not in RequestHandler._template_loaders:
loader = self.create_template_loader(template_path)
RequestHandler._template_loaders[template_path] = loader
else:
loader = RequestHandler._template_loaders[template_path]
t = loader.load(template_name)
namespace = self.get_template_namespace()
namespace.update(kwargs)
return t.generate(**namespace)
注意看
t.generate(**namespace)
namespace = 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.reverse_url
)
_=self.locale.translate
{{ _("New post") }}的意思是把New post這個字元串轉譯成本地語言即國際化 。
本地化
推薦閱讀:
※tornado的AsyncHTTPClient和requests庫為什麼不關閉連接?
※如何評價知乎開始將核心業務向 Go 技術棧遷移?
※如何理解Tornado中的協程模塊(gen.coroutine)?
※知乎為什麼要選擇用Tornado做為web開發框架,非同步非阻塞模式在此起到了作用?
※請問如何處理tornado模板和angular.js的 {{ }} 表達式衝突問題?
TAG:Tornado |