So, I'm sure this is just downright silly and has been done before,
but I'm a relative newbie to python and especially pylons (working
with it for 3 days now), so even the small accomplishments make me
very happy.  On the off chance that nobody's done anything like this
before, I thought I'd post this to help anyone down the line.

I'm intrigued by both Genshi and Mako as templating engines and just
can't decide which one I like more.  I really like the xml-ness of
Genshi, but at the same time, I like the easiness of Mako...  Thus, I
wanted to find some way to switch between the two rendering engines
simply by changing the template_engine in environment.py...  But when
I did that, I also had to change the url of each file loaded, and
genshi still wasn't serving as content_type = "xhtml" like I
wanted...  Switching between engines is simply a pain.  So, I added a
render engine dispatcher to /lib/base.py (in the BaseController
class):

======
    def render(self, page):
        """The render dispatcher.  Renders pages differently per
rendering engine.
        Render assumes that pages will be in "/some/page.extension"
url-format,
        not the "some.pageWithoutExtension" module-format used by
genshi/myghty."""
        render_engine = config["buffet.template_engines"][0]["engine"]
        renderer = getattr(self, "%s_render" % render_engine)
        return renderer(page)

    def genshi_render(self, page):
        """Renders pages with options necessary for the genshi
renderer.
        Transforms urls into module names, as genshi requires."""
        # I like pylons to serve my genshi pages as real xhtml.
        response.headers["Content-type"] = "application/xhtml+xml"
        return render(page.rpartition('.')[0].replace('/','.')[1:],
format="xml")

    def mako_render(self, page):
        """Renders pages with mako options."""
        # nothing necessary.
        return render(page)
======

To use this new render-engine dispatcher, you must simply go into the
controllers and change each occurrence of:
    return render("some/url.mako")
to:
    return self.render("some/url.mako")

The render engine dispatcher will take care of the rest.  My favorite
part?  It even allows different rendering options to be set site-wide
per rendering engine.  Now, thanks to Genshi's changed content-type, I
CAN actually serve my documents as actual XHTML as I intended (while
keeping Mako's templates as proper HTML 4.01 Strict, like I want).
More engines can be added simply by creating the proper method (i.e.,
Myghty).  The only trouble is that it requires template duplication,
but repetition reinforces the learning anyway.

What do people think?  Is this at all useful in any way?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to