Mark Fowler wrote:
> The site had a master map that was written in xml
> 
> <page name="Home" url="/">
>   <page name="Subsection1" url="/sub1">
>    <page name="Subsection2" url="/sub1/sub2">
>      <page name="CurrentPage" url="index.ttml">
>    </page>
>   </page>
> </page>

This is the important thing, I think.  It doesn't matter if your map
is XML, TT, or Perl, hand-built or auto-generated, as long as you have 
a map.

One relatively easy way to implement a breadcrumb trail (and other 
things...) is to organise your map, or build an index from it, that uses 
the url/directory path elements as hash indices pointing deeper into the 
structure.

Hopefully an example will make this clear.  We define a sitemap in a
pre-processed config file (or from Perl, a plugin, XML file, whatever).

   [% site = {
          url  = '/testing/123'
          page = {
              index = {
                  name = 'Home'
                  path = 'index.html'
              }
              download = {
                  name = 'Download'
                  path = 'download'
                  page = {
                      index = {
                          name = 'Download'
                          path = 'download/index.html'
                      }
                      stable = {
                          name = 'Stable Release'
                          path = 'download/stable.html'
                      }
                      devel = {
                          name = 'Developer Release'
                          path = 'download/devel.html'
                      }
                  }
              }
              # ...etc...
          }
      }
   %]

Each template page has a corresponding entry in the sitemap for 
each element of the path leading to it.

    index.html            => site.page.index
    download/stable.html  => site.page.download.page.stable
    download/devel.html   => site.page.download.page.devel
    # ...etc...

Then you examine template.name, strip off the .html suffix, split it into 
path elements, walk the sitemap tree, save each node that you visit in the 
breadcrumb trail, and you're done.

[%  # remove .html suffix and split on '/' 
    paths = template.name.replace('\.\w+$').split('\/');
    node  = site
    trail = [ site ]

    # walk the map to the current page
    WHILE paths.size;
        name = paths.shift;
        THROW path "no page '$name' in $template.name\n"
            UNLESS (node = node.page.$name);
        CALL trail.push(node);
    END
%]

Then you can print your trail:

    [% FOREACH crumb = trail %]
    <a href="[% site.url %]/[% crumb.page.index.path or crumb.path %]">
    [%- crumb.name -%]
    </a>
    [% ' / ' UNLESS loop.last %]
    [% END %]

Note the use of [% crumb.path.index.path or crumb.path %] so that we
generate URLs which point to the index page for a section (e.g.
/testing/123/download/index.html) in preference to URL which just points
to the directory (e.g. /testing/123/download).

There are many variations on this theme, and plenty of optimisations that
you can do to make life easier for yourself.  But you could probably fill
an entire chapter of a TT book on that subject...  :-)

Other things...

Another tip on a related subject is to use the template.name and the 
sitemap 'path' elements to implement menus that correctly highlight the
right option, depending on which page you're browsing.

For example:

    [% site.menu = [
           site.page.index
           site.section.download
           site.section.info
           site.section.and.so.on
       ]
  
       PROCESS menu menu=site.menu
    %]

menu:
    [% FOREACH item = menu;
         path = item.page.index.path or item.path;
         href = "$site.url/$path";
    
         IF path and template.name.match("^$path")
             # path matches - it's a selected item
    -%]
    <b><a href="[% href %]">[% item.name %]</a></b>
    [%   ELSE 
             # unselected item
    -%]
    <a href="[% href %]">[% item.name %]</a>
    [%   END;
       END
    %]
    
Each section can define its own menu, and you can theoretically nest them
to any depth.  But whenever you display a menu option in any menu, that
has a path corresponding to the current template, you get it correctly
highlighted as being on the active path.

HTH

A



_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.ourshack.com/mailman/listinfo/templates

Reply via email to