[Twisted-web] Nested Nevow templates
Matt Goodall
matt at pollenation.net
Thu Nov 4 15:28:37 MST 2004
On Thu, 2004-11-04 at 13:33 -0800, Erik Swan wrote:
> Everyone,
>
> I'm somewhat new to Nevow and have a simple question - btw, it maybe
> already possible and i missed the man page...
>
> We mostly use HTML based templates to drive our presentations as we have
> an html designer that can design and maintain a large majority of the UI
> without knowledge of any coding. The designer delivers html that can be
> rendered on its own for review but also adds in directives, patterns and
> slots to make the programmers life easier.
>
> My question centers around the best way to have nested templates - that
> is i'd like a directive that would function as an include and all the
> appropriate Nevow render code get executed automatically. That way our
> designer can build a header, footer, sidebars, and other html components
> as separate files and the main template just "includes" as appropriate.
> Something like
> <nevow:include name="blah.html">
WARNING: Experimental code alert. Use at your own risk. ;-)
This sounds like you want macros. There is a relatively simple
implementation in my sandbox ...
svn co svn://divmod.org/svn/Nevow/sandbox/mg/macro
The basic idea is that you have one (or more) XHTML files containing the
macros. The page templates include a macro by adding a n:render="macro
myMacro" attribute to some element.
Macros are located, via the LocateMacroMixin, as macro_* attributes of
the page (or more likely, some base page class). The macro_* attributes
can use anything (stan, xmlfile, whatever) to load and return the
template for the macro.
The interesting bit of all this is that any pattern inside the page's
macro-including element are pushed into the macro using fillSlots. Take
a look at how page.html specifies the <title> for the 'head' macro or
how the content is passed to the 'body' macro and you'll see what I
mean.
I've been using this technique for a while now and it works fairly well.
Whether it fits your needs I'm not sure but any feedback/improvements
would be gratefully accepted.
Note: I know this implementation can be improved, i.e. perhaps an
explicit n:macro tag to make things clearer and optional macro expansion
at precompile time.
> Additionally, and here is my Nevow ignorance, there must be a simple few
> lines of code that does the equivalent in code for example if i had:
> <nevow:render name = templateName>
>
> then in...
> def render_templateName(self, context, data):
> #load and render file templateName as include and return result
>
> Lastly, if the above method works OK then I'd rather have
> <nevow:render name=include nevow:arg=templateName>
> where i can have a generic include render method and take an argument
> from the html tag.
This is already possible using a parameterised renderer. In fact, the
macro renderer described above does exactly this. See the render_macro
function in macro.py.
Hope this helps.
Cheers, Matt
More information about the Twisted-web
mailing list