[Twisted-Python] WOVEN Question

Alex Levy mesozoic at polynode.com
Mon Jun 23 11:50:47 EDT 2003


On Mon, Jun 23, 2003 at 11:01:27AM +0200, Philippe Lafoucri?re wrote:
-- snip --
> macros that could expand when I want
-- snip --

I was making the same complaint in #twisted a couple weeks ago, and was
pointed to a wonderful piece of code: woven.widgets.ExpandMacro.  Check it
out; I've included a brief example below.

I have a macro file:

-- template.xhtml --

<html>
<head macro="head">
  <link rel="stylesheet" href="style.css"/>
  <title slot="title">Fill me</title>
</head>
<body macro="body">
  <h1>My site</h1>
  <div>Nav bar goes here</div>
  <div slot="content">Content</div>
</body>
</html>

-- end --

...and then my Page does something like this:

-- mypage.py --

class MyPage(page.Page):

  template = """
  <html>
  <head view="headMacro">
    <title fill-slot="title">My title</title>
  </head>
  <body view="bodyMacro">
    <div fill-slot="content">Whatever</div>
  </body>
  </html>"""
  
  def wvfactory_headMacro(self, request, node, m):
    return widgets.ExpandMacro(m,
                               macroFileDirectory = '/path/to/templates',
                               macroFile = 'template.xhtml',
                               macroName = 'head')

  def wvfactory_bodyMacro(self, request, node, m):
    return widgets.ExpandMacro(m,
                               macroFileDirectory = '/path/to/templates',
                               macroFile = 'template.xhtml',
                               macroName = 'body')

-- end --

I really like this functionality, and I think it should be pointed out in
the documentation.  Dunno if that's already done for 1.0.6 docs, but the
above code works with Twisted 1.0.5.

Hope this helps.

-- 
Alex Levy
WWW: http://mesozoic.geecs.org

"Never let your sense of morals prevent you from doing what is right."
 -- Salvor Hardin, Isaac Asimov's _Foundation_




More information about the Twisted-Python mailing list