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

Nuutti Kotivuori naked at iki.fi
Thu Sep 7 08:17:28 CDT 2006


Valentino Volonghi aka Dialtone wrote:
> On Thu, 07 Sep 2006 14:33:56 +0300, Nuutti Kotivuori <naked at iki.fi> wrote:
>> Nuutti Kotivuori wrote:
>> ,----[ 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.
>
> I have to say that this way of organizing code doesn't strike me as
> too effective. Also you seem to be misusing the Fragment object
> IMHO. It's pretty useless to call self.rend and then substituting
> the tag attribute (which was created by the rend object, just don't
> call rend at all and do something else).

Yes, I am misusing the Fragment object. The rend() method just
happened to be handy there. I'm getting rid of it.

> Anyway the real problem with this organization is encapsulation of
> the relevant methods, in my opinion it might become very hard to
> handle these very complex templates togheter with their
> methods. Unfortunately since I've never needed something like this I
> can't even come up with a valuable solution, sorry.

Well, I hope we can manage. Actually, with the latest bit of code I
wrote, I think this already satisfies the requirements I set for it
originally. Here's the changed part:

,----[ testsite.py ]
| class Snippets(rend.Fragment):
|     docFactory = loaders.xmlfile('snippets.xhtml')
| 
|     def patternContext(self, ctx, name):
|         context = WovenContext(parent=ctx)
|         self.rememberStuff(context)
|         # FIXME: preprocessors?
|         doc = self.docFactory.load(context)
|         context.tag = inevow.IQ(doc).onePattern(name)
|         return context
| 
|     def one(self, ctx):
|         return self.patternContext(ctx, 'one')
| 
|     def two(self, ctx, arg1, arg2):
|         context = self.patternContext(ctx, 'two')
|         context.tag.fillSlots('arg1', arg1).fillSlots('arg2', arg2)
|         return context
| 
|     def three(self, ctx):
|         return self.patternContext(ctx, 'three')
| 
|     def render_three_special(self, ctx, data):
|         import datetime
|         return 'special render with time %s' % datetime.datetime.now()
`----

Now the Fragment object basically only offers the rememberStuff() call
so I don't have to do that myself - and being a Data/Render/Macro
factory etc. Which is pretty much what I want from it anyway. If I
want to cache the pattern lookups, I can do it inside the
patternContext method easily, but I'll do that only if I find out it
ends up being a performance problem.

So, thanks for all the help and guidance. I'll bug people here more
when I run into some more problems.

-- Naked




More information about the Twisted-web mailing list