[英]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.