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

Valentino Volonghi aka Dialtone dialtone at divmod.com
Tue Sep 5 19:41:22 CDT 2006


On Tue, 05 Sep 2006 22:48:08 +0300, Nuutti Kotivuori <naked at iki.fi> wrote:
>Thanks! I've looked already at that and it seems nice... except for
>two points.
>
>First of all, the content is looked up like this:
>
>,----
>| def macro_content(self, ctx):
>|     return loaders.xmlfile(self.template+'.html', ignoreDocType=True,
>|                            templateDir=conf.TEMPLATE_PATH)
>`----
>
>Since I have several patterns in a single file, I would need to append
>pattern='content' keyword into that. But if I do that, then the file

I disagree. If you use a template as a pattern container than load it completely and use inevow.IQ() to get patterns from it. Patterns are not rendered anyway so just style the real content of the template and grab patterns togheter with it. Later on you'll be able to merge the patterns inside the rendering result.

>will get read once by every macro that uses a pattern from it? That

Once by every macro yes, but only once per macro, not once per request per macro. It's a very light cost.

>I'd like to avoid - I'd like to keep the loaders in a class
>variable nicely, or a Fragment, or something.
>Also, if I say pattern='content', then that loader will throw a hissy
>fit if the pattern isn't present. So I'd need a loader that would
>return some default value when the .load() method is called and that
>sounds like an icky solution again. No worries though, it's easy to
>hack.

You should not hack it. Nope, what if the default value is not there too?
Use a Fragment (or rather an Element instead). Besides that I don't really think that templates containing many patterns are a good way to deal with the problem, many templates each with a Fragment associated is a much better way to solve it.

>So, any ideas how I could have just one loader that would load the
>file once, and then just use normal nevow machinery to fetch the
>patterns from it (where it is easier to get a default value if the
>pattern does not exist)?

I told you:

loaded = loaders.xmlfile('file')
some_pattern = inevow.IQ(loaded).onePattern('whatever')

>Actually, I have a bit of a problem there - how do I get the whole
>document loaded in the methods I have? I can call
>self.docFactory.load() in every method ofcourse, but then the context
>is a bit screwy - however, I hesitate to pass in the context that is
>available at the time of the first call, since that might come from a
>different page each time and I'm not sure if the render/macro methods
>are going to be invoked from the correct object and such.

Don't pass the context, what's the problem here?

>I thought that the rendered/parsed version of the template resided in
>the loaders.xmlfile instance that is created - and if I return a new
>loaders.xmlfile instance (with a different pattern clause) from
>several different macros, then the page would get loaded as many
>times. Or if I returned a loaders.xmlfile instance from a render

No because the macro is not reloaded. Just try to modify a file that was compiled in macro. You'll see that unless you modify the file containing the macro nothing changes.

>method, then the file would be parsed on each rendition of the page -
>am I wrong?

Yes a bit.

>Patterns are optional yes, but if I call
>
>  loaders.xmlfile(filename, pattern='foobar')
>
>that will throw an exception if pattern 'foobar' is not present in the

Of course... You are telling it to search exactly for a particular pattern.

>file. So will onePattern(), but that is easily wrapped in a try/catch
>block (or I can use patternGenerator which does take a default
>argument in some cases) - where as a loader being wrapped doesn't do
>anything since the actual loading happens later (when the load()
>method is called).

correct.

It's not very clear what's your problem though. Maybe you should either show some code or try to solve it in the way you explained which seemed reasonable anyway.



More information about the Twisted-web mailing list