简体   繁体   English

自定义遍历和页面模板

[英]Custom traversal and page templates

Using Marius Gedminas's excellent blog post , I have created a custom traverser for a folder in my site. 使用Marius Gedminas的出色博客文章 ,我为网站中的文件夹创建了一个自定义遍历器。

This allows me to show: http://foo.com/folder/random_id 这使我可以显示: http://foo.com/folder/random_id : http://foo.com/folder/random_id

Instead of: http://foo.com/folder/object.html?id=random_id 而不是: http://foo.com/folder/object.html?id=random_id : http://foo.com/folder/object.html?id=random_id

The configuration side works great, I can catch the random_ids and search through my messages for the correct one, ready to display. 配置方面很好用,我可以捕获random_ids并在消息中搜索正确的消息,以供显示。

My problem is that I'm unsure how to then display the data via my usual page templates - at the TODO point in his original code ;) 我的问题是我不确定如何通过惯用的页面模板显示数据-在他原始代码的TODO点;)

if name == 'mycalendar':
            mycalendar = ... # TODO: do something to get the appropriate object
            return mycalendar

Usually I'd use something similar to: 通常我会用类似的东西:

class Test(BrowserPage):

    template = ViewPageTemplateFile('atest.pt')

    def __call__(self):
        return self.template()

But I can't work out how to do this correctly in the context of the custom traversal. 但是我无法解决如何在自定义遍历的上下文中正确执行此操作。


UPDATE : To be clear I want to avoid adding anything else to the url ( No : http://foo.com/folder/random_id/read ). 更新 :明确地说,我想避免在URL中添加其他任何内容( http : //foo.com/folder/random_id/read )。

I don't need the view to be available via any other address ( No : http://foo.com/folder/read ) 不需要通过任何其他地址查看该视图( http : //foo.com/folder/read

The ZCML for the view I'd like to use is: 我要使用的视图的ZCML是:

<browser:page
  for="foo.interfaces.IFooFolderContainer"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>

I'm guessing (on further advice), something along the lines of: 我猜(根据进一步的建议),大致类似于:

return getMultiAdapter((mycalendar, self.request), IPageTemplate, name=u'read')

Or even a default view for the object type (a dict in this case) that's being returned: 甚至返回要返回的对象类型的默认视图(在这种情况下为字典):

<browser:page
  for="dict"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>

It would be easier to answer your question if you showed what your custom traverser is doing. 如果您显示了自定义遍历程序正在做什么,将更容易回答您的问题。

Essentially, you want something like this: 本质上,您想要这样的东西:

def publishTraverse(self, request, name):
    if name in self.context:
        return MyMessageView(self.context[name], request)

    # fall back to views such as index.html
    view = queryMultiAdapter((self.context, request), name=name)
    if view is not None:
        return view

    # give up and return a 404 Not Found error page
    raise NotFound(self.context, name, request)

where MyMessageView can be something as simple as MyMessageView可以像

class MyMessageView(BrowserPage):
    __call__ = ViewPageTemplateFile('read.pt')

Disclaimer: I'm not sure if the view you instantiate directly will be protected by a security wrapper; 免责声明:我不确定您直接实例化的视图是否会受到安全包装的保护; make sure your functional tests ensure anonymous users can't view messages if that's what you want. 确保您的功能测试确保匿名用户无法查看邮件(如果您要这样做的话)。

If you end up at a proper object with your custom traverser, you can just tack on the template name and user "context" in that template. 如果您使用自定义遍历程序最终找到了合适的对象,则只需添加模板名称和该模板中的用户“上下文”即可。 So http://foo.com/folder/random_id/my_template and in the template do the normal <h1 tal:content="context/title" /> stuff. 因此, http://foo.com/folder/random_id/my_template并在模板中执行常规的<h1 tal:content="context/title" />东西。

IIUC, what you want is to render the 'read' view when somebody requests /folder/random_id. IIUC,您想要的是在有人请求/ folder / random_id时呈现“读取”视图。 If that's the case, all you need to do is make your traversal return an object (IFolderContent, maybe) representing a random_id and specify the 'view' page as the defaultView for IFolderContent. 在这种情况下,您所需要做的就是使遍历返回一个表示random_id的对象(可能是IFolderContent),并将“视图”页面指定为IFolderContent的defaultView。

The defaultView is needed because there's no view specified for the random_id object in your URL. 需要defaultView,因为您的URL中没有为random_id对象指定视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM