继承文件的访问属性

时间:2012-11-29 22:45:44

标签: python cherrypy mako

我有以下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}

将在包含lookup.get_template('foo.html')的Python文件中调用html文件。

我可以anotherAttribute访问lookup.get_template('foo.html').render(anotherAttribute='bar')。现在我想知道如何访问attribute中的base.html

1 个答案:

答案 0 :(得分:0)

您只能通过模块范围与“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}
相关问题