19 Jan 2011 zanee   » (Journeyer)

Plone and div blocking

Excuses

I haven't written any functional tutorial or such for Plone in a while. I'm working on a paper involving openid and started some work on an introduction to object and dynamic publishing that I need more time to work on, have random other things moving and etc excuses here. Something quick and useful in the meantime I suppose.

What is div blocking?

Div blocking is what I'm calling the process of returning a CoookedBody() back to your template into a specific html div. It's not specific to plone and it's a simple and straight-forward method many Plone developers call on. The idea is that you want to be able to return a section of html back into your overall template using a "Page" or "Document" content type. A simple use case for this is when you want to allow content-editors a chance to change something in the overall webpage using the standard Plone page content type. No sense in creating a new content-type just to return some formatted structured text. The end result is that you would like the final rendered html from that page to fit in-between <div></div> tags and for the overall look to leave your layout alone.

CookedBody() is a method that is apart of the overall Document object located in the Products.CMFDefault egg. It is an accessor method and it looks like this:

    #
    #   IDocument methods
    #   

    security.declareProtected(View, 'CookedBody')
    def CookedBody(self, stx_level=None, setlevel=0, rest_level=None):
        """ Get the "cooked" (ready for presentation) form of the text.

        The prepared basic rendering of an object.  For Documents, this
        means pre-rendered structured text, or what was between the
         tags of HTML.

        If the format is html, and 'stx_level' or 'rest_level' are not
        passed in or is the same as the object's current settings, return
        the cached cooked text.  Otherwise, recook.  If we recook and
        'setlevel' is true, then set the recooked text and stx_level or
        rest_level on the object.
        """
        if (
            (self.text_format == 'html' or self.text_format == 'plain'
            or (stx_level is None)
            or (stx_level == self._stx_level))
            and
            ((rest_level is None)
            or (rest_level == self._rest_level))
            ):
            return self.cooked_text
        elif rest_level is not None:
            cooked = ReST(self.text, initial_header_level=rest_level)
            if setlevel:
                self._rest_level = rest_level
                self.cooked_text = cooked
            return cooked
        else:
            cooked = stx2html(self.text, level=stx_level, header=0)
            if setlevel:
                self._stx_level = stx_level
                self.cooked_text = cooked
            return cooked

Reading the documentation of the object method we can see that it returns the cached cooked text which is stored in Plone's portal_catalog. One can go ahead and set stx_level or rest_level if you want which will use the reStructuredText parser to get you what you want.

I'm normally using a custom template in the form of a BrowserView and very rarely am I utilizing Plone's default theme. So if you know nothing about BrowserViews then please read the previous link first.

How to do?

Pretty simple, you just get the page object that you are looking for and run CookedBody(). So create a page and let's call it "Example" and fill it with some content. From there in creating your BrowserView lets add the method getTextFromPage() which will take the content from the page Example and return the CookedBody.

class MySpecialThemeView(BrowserView):

    implements(IMySpecialThemeView):

    def getTextFromPage(self):
    """Get the structured text from a page object with the id/name Example"""

        # We grab the root of our site here
        root = self.portal_eroor.getPortalObject()

        # We then grab the page object with the id Example
        example = getattr(root, "Example")

        # We then grab the body of the page as that is where the content lies
        # We could just as easily grab the title of the page or other attributes if needed
        body = example.CookedBody()

        # We then return body to our web browser
        return body

That finished our template would resemble something like:

Cooked Template HTML

HTML Template for CookedBody

Where right at the top of the page we have the div named example. That's pretty much it!

Share

Syndicated 2011-01-19 19:15:05 from Christopher Warner ยป Advogato

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!