Plone 4:向视图类(BrowserView)传递参数

6 投票
1 回答
1166 浏览
提问于 2025-04-17 12:01

我一直在参考这个网址,想要用BrowserView来创建模板视图。到目前为止,这个方法还不错,我已经能够创建一个带有视图类的模板。

我想知道的是,是否可以给视图类里的方法传递参数,也就是说:

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只是一个变量,可以是任何东西。它之前已经定义过了。

1 个回答

7

是的。

  <p tal:content="python:view.still_dreaming(item.publication_date)" />

在TAL表达式中,你可以使用TAL遍历语法(默认)、Python语法或者字符串语法。

http://collective-docs.readthedocs.org/en/latest/functionality/expressions.html

撰写回答