2013-05-19 42 views
2

我正在使用CherryPy和Mako模板。我試圖找出如何從最初的電話通aruguments(在這個例子中title):將變量從被調用的Mako模板傳遞給繼承模板

class Landing(object): 
    def index(self): 
     tmpl = lookup.get_template("index.html") 
     return tmpl.render(title="Hello World") 
    index.exposed = True 

index.html

<%inherit file="base.html"/> 
<%def name="title()">$(title)</%def> 
this is the body content 

,然後繼承base.html模板:

<!DOCTYPE html> 
    <head> 
     <meta charset="utf-8"> 
     <title>$(self.title())</title> 
    </head> 
    <body> 
     <h1>$(parent.title())</h1> 
     ${self.body()} 
    </body> 
</html> 

我試過self.titleparent.title,都沒有工作。我如何從初始調用中傳遞變量?

+1

$ {TITLE}應該工作。所以使用大括號而不是普通的。它不是函數調用,所以最後刪除()。 –

+0

您可能還想將name =「title()」更改爲name =「$ {title}」 –

+0

@basvandenberg謝謝,就是這樣!如果您將其添加爲答案,我會接受它。我嘗試用$ {title}替換title(),但是我得到了錯誤:'mako.exceptions.CompileException:標記'def'中的'attibute'name'不允許嵌入表達式在文件'template/index.html'中的行: 2 char:1' – xylar

回答

0

爲了使用呈現變量 - 您使用他們${ title }$(title)