我一直在关注this URL帮助我使用 BrowserView 创建模板 View 。到目前为止,它工作正常,我能够创建一个带有 View 类的模板。
我需要知道的是是否可以将参数传递给 View 类中的方法,即
from Products.Five import BrowserView
class Html(BrowserView):
def still_dreaming(self):
msg = "Some people are still dreaming"
return msg
我需要向 still_dreaming 函数添加一个额外的参数并在函数内部处理它,如下所示:
def still_dreaming(self, some_arg):
msg = some_arg + " Some people are still dreaming"
return msg
然后我需要调用该函数并从我的模板向它传递一个参数。像这样:
<p tal:content="view/still_dreaming(item/publication_date)"></p>
不幸的是,我不知道将参数传递给方法的正确方法。任何指针将不胜感激。
编辑:
item/publication_date
只是一些变量,可以是任何东西。它之前刚刚定义过。
最佳答案
是的。
<p tal:content="python:view.still_dreaming(item.publication_date)" />
您可以在 TAL 表达式中使用 TAL 遍历语法(默认)、Python 语法或字符串语法。
http://collective-docs.readthedocs.org/en/latest/functionality/expressions.html
关于python - Plone 4 : Passing arguments to view class (BrowserView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079019/