[英]Condition mako base template from inheriting one
我有一个带有if语句的base.mako模板,是否包含jQuery
<head>
% if getattr(c, 'includeJQuery', False):
<script type="text/javascript" src="jquery.js"></script>
% endif
...
有几个模板是从base.mako继承的,有人需要jQuery,有人不需要。
目前,我必须在调用render之前在控制器中设置属性
c.includeJQuery = True
return render('/jQueryTemplate.mako')
但是我认为这应该直接放在子模板中(即jQueryTemplate.mako)
我尝试在继承之前添加它
<% c.includeJQuery = True %>
<%inherit file="/base.mako"/>\
但它不起作用。
有小费吗?
谢谢你的支持
您不应在模板中使用“ c”。
<% includeJquery = True %>
和
% if includeJquery:
...
% endif
应该足够了。
我认为您做错了...在基本模板中,应为jquery块创建一个空白def并调用它。 然后在继承的模板中,只需重新定义块即可。
base.mako:
<head>
${self.jquery()}
</head>
<%def name="jquery()"></%def>
然后在另一个模板中,添加带有以下内容的jquery:
<%inherit file="base.mako />
<%def name="jquery()">
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
</%def>
好吧,自从有了
<script type="text/javascript" src="jquery.js"></script>
我还需要添加其他一些js,并将jQueryScript%def放在子模板中
##jQueryTemplate.mako
<%def name="jQueryScript()">
<script>
</script>
</%def>
然后在基地我检查是否存在并相应地添加
#base.mako
%if hasattr(next, 'jQueryScript'):
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
${next.jQueryScript()}
%endif
因此我不需要在控制器中设置任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.