可能%包含文件在Mako中是否存在文件?
问题描述:
我正在使用<%include file="special_message.html" />
在页面上包含任何必需的特殊消息(例如关于系统停机时间的消息)。可能%包含文件在Mako中是否存在文件?
我希望能够通过将消息HTML放入special_message.html
文件并通过简单地删除该文件将其关闭。
但是,如果我删除文件,Mako将失败并显示cannot locate template
消息。有没有什么办法(使用空文件不足)告诉Mako如果没有找到%include
文件,则只包含任何内容?
答
此生的Python模块可以做的伎俩......
<%
from mako.exceptions import TemplateLookupException
try:
tmpl = self.get_template("special_message.html")
except TemplateLookupException:
pass
else:
tmpl.render_context(context)
%>