How to effectively reuse viewlets?

View: New views
8 Messages — Rating Filter:   Alert me  

How to effectively reuse viewlets?

by prinzdezibel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi guys, I stumbled upon a problem when doing my first steps with grok/zope.
Maybe somebody of you smart people can help me!
See, I wanted to reuse an existing viewlet that is contained in a package I don't own and I don't want to modify. Basically I want to have this very viewlet handled by another viewletmanager. Therefore I subclassed the viewlet and tried to register it to a different viewletmanager via grok.viewletmanager(MyViewletManager)

The problem is now that my viewlet is rendered twice. From the original viewletmanager and from mine. How can I prevent the viewlet to be rendered from it's original viewletmanager which is placed in the mentioned package I don't own. Is it possible to unregister a viewlet from its manager?

Thank you for your help!

Michael.

Re: How to effectively reuse viewlets?

by Martin Aspeli-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

prinzdezibel wrote:

> Hi guys, I stumbled upon a problem when doing my first steps with grok/zope.
> Maybe somebody of you smart people can help me!
> See, I wanted to reuse an existing viewlet that is contained in a package I
> don't own and I don't want to modify. Basically I want to have this very
> viewlet handled by another viewletmanager. Therefore I subclassed the
> viewlet and tried to register it to a different viewletmanager via
> grok.viewletmanager(MyViewletManager)
>
> The problem is now that my viewlet is rendered twice. From the original
> viewletmanager and from mine. How can I prevent the viewlet to be rendered
> from it's original viewletmanager which is placed in the mentioned package I
> don't own. Is it possible to unregister a viewlet from its manager?

You can register a new viewlet with the same name for a more specific
interface (e.g. a browser layer) and renders to nothing.

Plone has other solutions (its viewlet managers allow re-ordering and
turning off of viewlets), but that's the simplest way in plain Zope.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book

_______________________________________________
Grok-dev mailing list
Grok-dev@...
https://mail.zope.org/mailman/listinfo/grok-dev

Re: How to effectively reuse viewlets?

by prinzdezibel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Martin Aspeli-3 wrote:
prinzdezibel wrote:
> Hi guys, I stumbled upon a problem when doing my first steps with grok/zope.
> Maybe somebody of you smart people can help me!
> See, I wanted to reuse an existing viewlet that is contained in a package I
> don't own and I don't want to modify. Basically I want to have this very
> viewlet handled by another viewletmanager. Therefore I subclassed the
> viewlet and tried to register it to a different viewletmanager via
> grok.viewletmanager(MyViewletManager)
>
> The problem is now that my viewlet is rendered twice. From the original
> viewletmanager and from mine. How can I prevent the viewlet to be rendered
> from it's original viewletmanager which is placed in the mentioned package I
> don't own. Is it possible to unregister a viewlet from its manager?

You can register a new viewlet with the same name for a more specific
interface (e.g. a browser layer) and renders to nothing.

Plone has other solutions (its viewlet managers allow re-ordering and
turning off of viewlets), but that's the simplest way in plain Zope.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book

_______________________________________________
Grok-dev mailing list
Grok-dev@zope.org
https://mail.zope.org/mailman/listinfo/grok-dev
I Martin,
thanks for your reply. Could you please write down a sample snippet of code for your suggested solution. Because I tried to override the render method of the viewlet in my derived viewlet and got a ambiguous render method error, because my original viewlet is rendered from a template.
Thanks.

Re: How to effectively reuse viewlets?

by Martin Aspeli-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

prinzdezibel wrote:

> I Martin,
> thanks for your reply. Could you please write down a sample snippet of code
> for your suggested solution. Because I tried to override the render method
> of the viewlet in my derived viewlet and got a ambiguous render method
> error, because my original viewlet is rendered from a template.

Viewlets are rendered by name. What I'm saying is, you do something like:

class BlankViewlet(grok.Viewlet):
     grok.name('the-original-viewlet-name')
     grok.viewletmanager(ITheOriginalViewletManager)
     grok.layer(ISomethingSpecificToYou)
     grok.context(ITheOriginalContext)
     grok.require('zope.View')

     def update(self):
         pass

     def render(self):
         return ''

The key here is that you copy the directives from the original, but you
use a skin-specific layer to override it, then you make it render to an
empty string.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book

_______________________________________________
Grok-dev mailing list
Grok-dev@...
https://mail.zope.org/mailman/listinfo/grok-dev

Re: How to effectively reuse viewlets?

by prinzdezibel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you!

It worked. Wouldn't it be good to have something more concise in grok to achieve this? It's a little bit verbose and it's not necessarily clear what the intention is.

Re: How to effectively reuse viewlets?

by Martin Aspeli-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

prinzdezibel wrote:

> It worked. Wouldn't it be good to have something more concise in grok to
> achieve this? It's a little bit verbose and it's not necessarily clear what
> the intention is.

Viewlets kinda suck, to be honest. They are useful for certain use
cases, but as a general way to build composite UIs, they suffer from
some limitations.

The way Plone solves this is to have a viewlet manager that consults a
persistent (site-local) registry on (a) which viewlets to hide and (b)
in which order to show the remaining viewlets. There's a GUI to hide and
re-order things (@@manage-viewlets). This solves a few problems, but
realistically, it's still not great.

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book

_______________________________________________
Grok-dev mailing list
Grok-dev@...
https://mail.zope.org/mailman/listinfo/grok-dev

Re: How to effectively reuse viewlets?

by Jeroen Michiel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Martin Aspeli-3 wrote:
Viewlets kinda suck, to be honest. They are useful for certain use
cases, but as a general way to build composite UIs, they suffer from
some limitations.
What are the alternatives? I am creating a fairly complex project, and indeed, viewlets can get in your way, but I got them to work. I read all the tutorials and howtos here, and based on that it seemed to me that viewlets were the way to go for more comple UIs. I don't think I really came across any alternatives mentioned on the grok site, apart from macro's, but that looked to be even more trouble and code repetition.

The main problem I have with viewlets is rendering forms. I have a specific layout that I want to see repeated on all pages. There is no such thing as a 'Formlet', so I first define the view in which the form should be rendered, I then define a form (e.g. grok.AddForm) that renders the form and defines the needed actions, and finally i define a viewlet that forwards the rendering to the form class. This is quite convoluted... Is there a better solution?

Re: How to effectively reuse viewlets?

by Martin Aspeli-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jeroen Michiel wrote:

>
> Martin Aspeli-3 wrote:
>> Viewlets kinda suck, to be honest. They are useful for certain use
>> cases, but as a general way to build composite UIs, they suffer from
>> some limitations.
>>
>
> What are the alternatives? I am creating a fairly complex project, and
> indeed, viewlets can get in your way, but I got them to work. I read all the
> tutorials and howtos here, and based on that it seemed to me that viewlets
> were the way to go for more comple UIs. I don't think I really came across
> any alternatives mentioned on the grok site, apart from macro's, but that
> looked to be even more trouble and code repetition.
>
> The main problem I have with viewlets is rendering forms. I have a specific
> layout that I want to see repeated on all pages. There is no such thing as a
> 'Formlet', so I first define the view in which the form should be rendered,
> I then define a form (e.g. grok.AddForm) that renders the form and defines
> the needed actions, and finally i define a viewlet that forwards the
> rendering to the form class. This is quite convoluted... Is there a better
> solution?

I think someone else will need to answer this question for Grok - I'm
not sure what people normally do. :-/

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book

_______________________________________________
Grok-dev mailing list
Grok-dev@...
https://mail.zope.org/mailman/listinfo/grok-dev