views

browserview,指的是一個頁面,與content type不同,browserview是一個單獨存在的頁面,

從plone 4.3 以後,由於plone預設採用了five.grok支援,所以要在plone上建立一個browserview是很容易的,依以下步驟:

1.在模組根目錄下,建立一個目錄browser

2.在package的根目錄下的configure.zcml 新增以下一行內容

<include package=".browser" />

3.在browser目錄下要有二個檔案,

configure.zcml , 是要定義頁面用的,

__init__.py , 空的內容,但一定要有這個檔案,five.grok會去scan目錄裏的檔案,找不到__init__.py,會顯示錯誤

4. configure.zcml除了表頭,定義頁面的的內容如下:

<browser:page
    for="*"
    name="testFunction"
    permission="cmf.ManagePortal"
    class=".testfunction.TestFunction" />

說明:name會成為網址的一部份,也就是 http://example.com/testFunction, class會去找testfunction.py的 class TestFunction

各項內容的詳細說明可以見下面連結

http://docs.plone.org/develop/plone/views/browserviews.html#creating-and-registering-a-view

最後,在程式部份,testfunction.py的內容如下

from Products.Five.browser import BrowserView

class TestFunction(BrowserView):
    def __call__(self):
        '''程式碼寫在這裏'''
        return

如果要使用template,則寫法如下

from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

class TestFunction(BrowserView):
    template = ViewPageTemplateFile('templatefile.pt')

    def __call__(self):
        '''程式碼寫在這裏'''
        self.myvar = "Test string"
        return self.template()

在 templatefile.pt 中,可以使用 view/myvar 來引用程式中的 self.myvar.

原文參考