Re: [Templates] "global" references?

[prev] [thread] [next] [Date index for 2005/05/25]

From: Steve Kelem
Subject: Re: [Templates] "global" references?
Date: 05:25 on 25 May 2005
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<div class="moz-text-html" lang="x-western"> Thanks.&nbsp; It mostly works.&nbsp;
I ran into a problem.&nbsp; If my map looks like:<br>
<pre>A
  A1
    A1_a
    A1_b
  A2
    A2_a
    A2_b
B
  B1
    B1_a
  B2
    B2_a
</pre>
If I'm processing a link_to_page to A2 from A.A2.A2_a, it works okay.<br>
If I try to link to A1_a, link_to_page complains: "menu error - Failed
to find menu link.<br>
This is because I followed the badger book example for sitemap.xml on
page 426, where the id of the first (sub)menu &amp; (sub)page has an
id="index".&nbsp; So the ids are not unique, but the names are.<br>
I changed the line:<br>
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NEXT UNLESS id == target_id;</pre>
to<br>
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NEXT UNLESS item.name == target_id;</pre>
and that helped.<br>
<br>
However, I still have a problem.<br>
From page A.A2, I cannot link to A.A1.anything.&nbsp; There is no error
message, and the generated link is empty (href=""), but the link text
is okay. Linking to B is okay.&nbsp; Linking to B.B1 is not okay. Neither is
B.B1.B1_a or B.B2 or B.B2.B2_a or B.B1.B1_a.<br>
<br>
Any ideas?&nbsp; How is the recursion done in your filter?<br>
<br>
Thanks for any help you can give.<br>
<br>
Steve<br>
<br>
Bill Moseley said the following on 5/24/2005 12:46 PM:
<blockquote cite="mid20050524194605.GA13177@xxxx.xxx" type="cite">
  <pre wrap="">On Mon, May 23, 2005 at 06:37:55PM -0700, Steve Kelem wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">I'm building a hierarchy of web pages.
Is there a way, in the template toolkit, to make references/links to
places in web pages somewhere in the hierarchy without knowing exactly
where they are, knowing just the name of the link?  I'd like to avoid 
having to know exactly where in the hierarchy the target of a link is, 
sort of how the xml map works.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
I use a modified version of the site map described in chapter 11 of
the Badger book (~p 423) to define the layout of pages.

Then I have a macro:

   MACRO link_to_page(target, text, return_link_only ) INCLUDE menu/find_menu_link;

that walks the map looking for the link.  And in a template I do:

   Read [% link_to_page('install#tips', 'these important tips') %]

Which allows custom text, or:

    Please see the [% link_to_page( 'foo' ) %] page.

which uses the text in the map.


There's a few pending TODO, but here's the macro.
Corrections/suggestions are alwasy welcome...


:r menu/find_menu_link


[%
    # Macro passes in 
    #   "target"            a menu id in config/map
    #   "text"              optional text to use instead of link text
    #   "return_link_only   optional flag to say return only the href
    # Link text is NOT html escaped.  Href is also not correctly escaped.
    # Note that there's nothing to prevent a map/menu from having two menu items
    # at different levels from having the same id.  Therefore, this processes each
    # menu level before recursing, so it should find top level ids first.

    # TODO
    # - allow passing in a tooltip (title)
    # - set class attribute saying if internal or external link.


    SET global.link_found = 0;  # to track when found so can stop recursing


    SET fragment    = target.replace('^[^#]+','');
    SET target_id   = target.replace('#.+$','');

    link = INCLUDE look_for_link;

    THROW 'menu', "Failed to find menu link for target '$target_id' from page $template.name"
        UNLESS link;
    link;
-%]
[%- BLOCK look_for_link;

    # Finds an entry in the site map. Call with the link_to_page(id) macro
    # TODO - make abort if not found

    subdirs = [];

    FOREACH id = map.page.keys;
        item = map.page.$id;

        # Is there a sub menu that should be looked through later?
        subdirs.push( item ) IF item.page;

        # Is this the item we are looking for?
        NEXT UNLESS id == target_id;

        global.link_found = 1;

        SET href = "${item.url}$fragment";

        IF return_link_only;
            href;
            RETURN;
        END;

        "&lt;a href=\"$href\"";
        " title=\"${item.tooltip}\"" IF item.tooltip;
        "&gt;";
        text || item.name;
        "&lt;/a&gt;";


        RETURN;
    END;


    FOREACH item = subdirs;
        LAST IF global.link_found;
        INCLUDE look_for_link map = item;
    END;

END -%]

  </pre>
</blockquote>
<br>
</div>
</body>
</html>

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] "global" references?
Steve Kelem 01:37 on 24 May 2005

Re: [Templates] "global" references?
Bill Moseley 19:46 on 24 May 2005

Re: [Templates] "global" references?
Steve Kelem 05:25 on 25 May 2005

Generated at 20:13 on 05 Jun 2005 by mariachi v0.52