[Twisted-web] Re: Nevow and template like files

Nuutti Kotivuori naked at iki.fi
Thu Sep 7 06:33:56 CDT 2006


Nuutti Kotivuori wrote:
> I'm diving in the snippet part next.

Okay. Now I have my first try at the snippet problem.

,----[ datapage.xhtml ]
| <?xml version="1.0"?>
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml"
|   xmlns:n="http://nevow.com/ns/nevow/0.1">
|   <head>
|     <title><n:invisible n:pattern="title">Data Page</n:invisible></title>
|     <n:invisible n:pattern="head" />
|   </head>
|   <body>
|     <n:invisible n:pattern="content">
|       <p>
|       <n:invisible n:macro="test">
|           Replaced by test data.
|         </n:invisible>
|       </p>
|       <p>
|         <n:invisible n:macro="snippet_one">
|           Snippet one.
|         </n:invisible>
|       </p>
|       <p>
|         <n:invisible n:macro="snippet_two">
|           Snippet two.
|         </n:invisible>
|       </p>
|       <p>
|         <n:invisible n:macro="snippet_three">
|           Snippet three.
|         </n:invisible>
|       </p>
|     </n:invisible>
|   </body>
| </html>
`----

,----[ snippets.xhtml ]
| <?xml version="1.0"?>
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml"
|   xmlns:n="http://nevow.com/ns/nevow/0.1">
|   <head>
|     <title>Snippet Page</title>
|   </head>
|   <body>
|     <n:invisible n:pattern="one">
|       <p>Test snippet one.</p>
|     </n:invisible>
|     <n:invisible n:pattern="two">
|       <p>Test snippet two with <n:slot name="arg1">argument
|       1</n:slot> and <n:slot name="arg2">argument 2</n:slot>.</p>
|     </n:invisible>
|     <n:invisible n:pattern="three">
|       <p>Test snippet three with a <n:invisible
|       n:render="three_special">render</n:invisible>.</p>
|     </n:invisible>
|   </body>
| </html>
`----

,----[ datapage.py ]
| class DataPage(TestPage):
|     datapageFactory = loaders.xmlfile('datapage.xhtml')
| 
|     def macro_test(self, ctx):
|         return 'Test successful.'
| 
|     def macro_snippet_one(self, ctx):
|         return Snippets().one(ctx)
| 
|     def macro_snippet_two(self, ctx):
|         return Snippets().two(ctx, 'foo', 'bar')
| 
|     def macro_snippet_three(self, ctx):
|         return Snippets().three(ctx)
| 
| class Snippets(rend.Fragment):
|     docFactory = loaders.xmlfile('snippets.xhtml')
| 
|     def one(self, ctx):
|         context = self.rend(ctx, None)
|         context.tag = inevow.IQ(context.tag).onePattern('one')
|         return context
| 
|     def two(self, ctx, arg1, arg2):
|         context = self.rend(ctx, None)
|         context.tag = inevow.IQ(context.tag).onePattern('two')
|         context.tag = context.tag.fillSlots('arg1', arg1)
|                                  .fillSlots('arg2', arg2)
|         return context
| 
|     def three(self, ctx):
|         context = self.rend(ctx, None)
|         context.tag = inevow.IQ(context.tag).onePattern('three')
|         return context
| 
|     def render_three_special(self, ctx, data):
|         import datetime
|         return 'special render with time %s' % datetime.datetime.now()
`----

Now, this method seems to work, but I'm not sure if I'm too happy with
it yet.

-- Naked




More information about the Twisted-web mailing list