Plone 5.2 rendering viewlet by name Plone Docs example no longer working

Hello. I was using Plone 5.0.6 several months ago, but when moving from Plone 5.1 to 5.2, I found that the example for rendering viewlets by name no longer works.

See: https://docs.plone.org/develop/plone/views/viewlets.html#rendering-viewlet-by-name

I already have described the problem here: After upgrade to Plone 5.2, the setupViewletByName from Plone Docs doesn't work

I was able to fix it for my custom viewlet by making sure to inherit from Acquisition.Implicit mix-in, but now I want to render the plone.belowcontenttitle.documentbyline viewlet in a customized view. I do not want to override the DocumentByLineViewlet class if I do not have to. Specifically I am having a difficult time understanding why the viewlet as produced by the factory can no longer be wrapped by the context in the function as detailed in the Plone 5 docs.

    def setupViewletByName(self, name):
        """ Constructs a viewlet instance by its name.

        Viewlet update() and render() method are not called.

        @return: Viewlet instance of None if viewlet with name does not exist
        """
        context = aq_inner(self.context)
        request = self.request

        # Perform viewlet regisration look-up
        # from adapters registry
        reg = self.getViewletByName(name)
        if reg == None:
            return None

        # factory method is responsible for creating the viewlet instance
        factory = reg.factory

        # Create viewlet and put it to the acquisition chain
        # Viewlet need initialization parameters: context, request, view
        try:
            viewlet = factory(context, request, self, None).__of__(context)
        except TypeError:
            # Bad constructor call parameters
            raise RuntimeError(
                "Unable to initialize viewlet {}. Factory method {} call failed."
                    .format(name, str(factory)))

This line: viewlet = factory(context, request, self, None).__of__(context) results in AttributeError: 'DocumentByLineViewlet' object has no attribute '__of__'. First, since I see it is still an issue, I wanted to raise this to see if anyone had found a full solution for rendering existing viewlets by name. The provider syntax that is included in the doc also did not work for the document by line. That is why I fell back to this method in the first place. So any insight will help a great deal. Thanks in advance!

1 Like

The __of__ may no longer be necessary. It was the old way to place the viewlet into a context able to check permissions. Now, the permission machinery supports other ways to achieve the same.

If you do find an example that works, would you please submit a PR to update the docs?