[Templates] Recursion in macros?

darren chamberlain dlc@users.sourceforge.net
Fri, 17 Jan 2003 08:01:11 -0500


* lmb <lars@marowsky-bree.de> [2003-01-16 17:39]:
> now my MetaCache module is working quite nicely - links change the URL
> automatically whereever I move the page, language icons can be set,
> the title of the page is consistent across the entire site,
> "referenced by" lists show up automatically when I add a link
> anywhere, I can query a list of pages which have a particular
> attribute set etc.

Nice.  Sounds interesting; what are the chances that you'll make this
stuff publicly available?

> And I am wondering how to best generate the navigation completely
> automatically. This amounts to a tree traversal; the obvious way to do
> this would be a recursive macro ;-)

Randal Schwartz had a column on this at one point, I believe, and he's
posted his templates and code to the list; I'll see if I can dig up a
reference to it.

> But I doubt that MACROs are recursion safe, are they?

They are, since they are implemented as references to standard Perl
subroutines, provided you terminate your recursion.  Note that because
they are implemented as subrefs, they are *not* subject to TT's normal
recursion checks.

I've had templates with MACROs that unintentionally called themselves (a
typo in a MACRO named I<nav> intending to reference a variable named
I<navs>, but accidentally referencing I<nav>) bring down entire systems
-- be sure you write your code carefully.  Such problems are hard to
trace, especially when the macros were written by someone else. :)

[-- snip --]
> An even _further_ option would be to have the Perl function return a
> specially formatted data structure which I could traverse with a
> FOREACH loop and where each step contained information of whether to
> go up a level (ie, close a </UL>) or go down one further (start a new
> <UL>).

This is the best way, I think; encapsulate the ugliness within straight
perl (write a File::Find::Rule subclass that is META aware, for
example), and then isolate your display code to a template.

> Oh, and while I'm at it, I saw a recent discussion on Image modules;
> does anyone have a nice module for generating image galleries etc?

I think there's some on Freshmeat written in PHP... ;)

> Sincerely,
> 	Lars
> 
> -- 
> Principal Squirrel 
  ^^^^^^^^^^^^^^^^^^

Is that your *real* title?

(darren)

-- 
Hard as it is to believe, sometimes bloody, proletariat revolution
just isn't the answer.  Like in Algebra class, for example.
    -- Don Swain