【问题标题】:Access attribute of inherited file继承文件的访问属性
【发布时间】:2012-11-29 22:45:43
【问题描述】:

我有以下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。现在我想知道如何访问base.html 中的attribute

【问题讨论】:

    标签: python cherrypy mako


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2014-11-10
      • 2017-09-01
      • 2019-03-08
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      相关资源
      最近更新 更多