1

I know, it's a naive question :-)

Originally poped up into my mind over Zope 2: How to properly “browser:page” to make a page available everywhere?

Community
  • 1
  • 1
BahmanM
  • 1,420
  • 10
  • 18

1 Answers1

1

Views are callable adapters that provide output based on the context and the request.

Templates are callables that render a piece of text based on a template. They are often used in views.

Note that views can return text without using a template:

from zope.publisher.browser import BrowserView

class MyView(BrowserView):
    def __call__(self):
        return "Hello world, I am located at {0}".format(self.context.absolute_url())

Views can also be used by other Zope code, without themselves being published. Zope code uses a lot of views internally.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So a `view` handles actions defined in a piece of HTML which is generated by a (or multiple) `template`(s). And those actions change the underlying model (like `OFS.Folder`), correct? – BahmanM Jan 08 '13 at 13:10
  • They *could* do that, but don't have to. But yes, the context could be a `OFS.Folder`. – Martijn Pieters Jan 08 '13 at 13:11
  • Then why do we need a new layer (`view`)? I don't understand the purpose of `view`s. Indulge me a moment in my confusion but I haven't seen a similar pattern in other web frameworks which are usually in the form of Template→HTML→Action→Model. – BahmanM Jan 08 '13 at 13:27
  • 1
    Because you can adapt views to different contexts. They are independent of the context and can be registered for additional contexts, *or* you can add additional interfaces to a context to make more views work for it. – Martijn Pieters Jan 08 '13 at 13:28
  • Put a checkbox in a form that adds or removes the `IScreenPresentation` interface from a context, and then the `ScreenPresentationView` view works or doesn't work, for example. – Martijn Pieters Jan 08 '13 at 13:29
  • Because a `view` interacts with `context`, it's not bound to any specific model. All it needs to know about the model, is its interface, regardless of the implementation. Am I right? BTW, is there a (simple) piece of code that demonstrates the use of views alongside templates in action? Shoot! I'm good at code reading :-) – BahmanM Jan 08 '13 at 13:39
  • There are *loads* of views defined in the [Plone ecosystem](https://github.com/plone); most packages will have a `browser` subdirectory that is going to be chuck-full of examples. – Martijn Pieters Jan 08 '13 at 14:32