继承文件的访问属性
问题描述:
我有以下base.html
文件,它作为每个其他继承它的文件的主要骨架。继承文件的访问属性
<%! import cherrypy %>
<!DOCTYPE html>
<html>
<body>
<div class="container">
<% attribute='defaultValue' %>
${attribute}
<p>
${self.body()}
</p>
</div>
</body>
</html>
现在我有一个继承了base.html
另一个文件,允许将其命名为foo.html
:
<%inherit file="base.html" />
Whatever... ${anotherAttribute}
的HTML文件将在Python文件,lookup.get_template('foo.html')
被调用。
我可以用lookup.get_template('foo.html').render(anotherAttribute='bar')
访问anotherAttribute
。现在我想知道如何访问中的attribute
?
答
您可以通过模块的范围一样,只有共享属性与 “ATTR”:
base.html文件
<%!
attribute = 'defaultvalue'
%>
foo.html
${self.attr.attribute}
否则base.html文件将需要直接将它传递给foo:
base.html
<%
attribute = 'defaultvalue'
%>
${self.body(attribute=attribute)}
foo.html
<%page args="attribute"/>
${attribute}