On Fri, Apr 19, 2002 at 10:16:16AM +0100, Dave Hodgkinson wrote:
> If've got a template that now needs refactoring. What I want is a
> degree of inheritance. Kind of. I want a "default" SQL query and
> output template, that I can then override based on what
> section/subsection we're in on the site.
This is where views can be useful.
[% VIEW boring_view
prefix='boring/';
END;
VIEW thrilling_view
prefix = 'thrilling/'
base = boring_view;
END;
myview = section.is_thrilling ? thrilling_view : boring_view
%]
When you say:
[% myview.header(title = page.title) %]
Then, if the section is thrilling, you'll get 'thrilling/header' if it
exists, otherwise defaulting to the base view which gives you 'boring/header'.
If the section is boring then you'll just get 'boring/header'.
Also consider the option of writing your own view-like plugin which
encapsulates the display logic for your site. It's really not hard
- see Template::View for an example, in particular the template()
and include() methods.
You might use it something like this:
[% USE mysite %]
[% mysite.include('header', title=page.title) %]
The idea is that the include() method just fetches a template
(via the template() method, which can do any kind of path translation
you like) and processes it. You could also add an AUTOLOAD method
which does the same, allowing you to call:
[% mysite.header(title=page.title) %]
In using a plugin this way, you encapsulate away the particular logic
that defines what templates should get loaded from where under what
conditions. You can change the plugin at any time to implement some
different strategy but your templates shouldn't need to change at all.
This, of course, requires a little more effort in setup/coding time,
but the extra simplicity and future proofing that it can bring to your
page templates can be worth.
A