3.5.4 Nesting Macros
Description: Nesting Macros
Keywords: albatross

##
3.5.4 Nesting Macros

Let's revisit the http://slashdot.org/ HTML for a story and see how to use macros to assist in formatting the entire story summary.

Consider the rest of the story summary minus the header (reformatted to fit on the page):

    <a HREF="http://slashdot.org/search.pl?topic=microsoft">
    <img SRC="http://images.slashdot.org/topics/topicms.gif" WIDTH="75" HEIGHT="55"
    	BORDER="0" ALIGN="RIGHT" HSPACE="20" VSPACE="10" ALT="Microsoft">
    </a>
    <b>Posted by <a HREF="http://www.monkey.org/~timothy">timothy</a>
     on  Sunday July 22, @11:32PM</b><br>
    <font size=2><b>from the hide-the-children-get-the-gun dept.</b></font><br>
    Goldberg's Pants writes: <i>
    "<a HREF="http://www.zdnet.com/zdnn/stories/news/0,4586,2792260,00.html?chkpt=zdnnp1tp02">ZDNet</a>
    and <a HREF="http://www.wired.com/news/technology/0,1282,45427,00.html">Wired</a>
    are both reporting on a new virus that spreads via Outlook. Nothing
    particularly original there, except this virus is pretty unique both
    in how it operates, and what it does, such as emailing random
    documents from your harddrive to people in your address book, and
    hiding itself in the recycle bin which is rarely checked by virus
    scanners."</i> I talked by phone with a user whose machine seemed
    determined to send me many megabytes of this virus 206k at a time; he
    was surprised to find that his machine was infected, as most people
    probably would be. The anti-virus makers have patches, if you are
    running an operating system which needs them.

The first task is to simplify is the topic specific image. There are a finite number of topics, and the set of topics does not change much over time. We could make effective use of the Albatross `<al-lookup>` tag to simplify this (`<al-lookup>` is discussed in section 3.6):

    <al-lookup name="story-topic">
     <al-item expr="'microsoft'">
      <a HREF="http://slashdot.org/search.pl?topic=microsoft">
       <img SRC="http://images.slashdot.org/topics/topicms.gif" WIDTH="75"
            HEIGHT="55" BORDER="0" ALIGN="RIGHT" HSPACE="20" VSPACE="10"
            ALT="Microsoft">
      </a>
     </al-item>
     <al-item expr="'news'">
       :
     </al-item>
    </al-lookup>

Then to display the HTML for the story topic all we would need to do is the following:

    <al-value expr="story.topic" lookup="story-topic">

Next we will simplify the acknowledgement segment:

    <b>Posted by <al-value expr="story.poster" noescape>
     on <al-value expr="story.date" date="%A %B %d, @%I:%M%p"></b><br>
    <font size=2><b>from the <al-value expr="story.dept"> dept.</b></font><br>

Finally we can bring all of these fragments together like this:

    <al-macro name="story-summary">
     <al-expand name="story-title"><al-value name="story.title"></al-expand>
     <al-value expr="story.topic" lookup="story-topic">
     <b>Posted by <al-value expr="story.poster">
     on <al-value expr="story.date" date="%A %B %d, @%I:%M%p"></b><br>
     <font size=2><b>from the <al-value expr="story.dept"> dept.</b></font><br>
     <al-value expr="story.summary" noescape>
    </al-macro>

Having defined the macro for formatting a story summary we can format a list of story summaries like this:

    <al-for iter="i" expr="summary_list">
     <al-exec expr="story = i.value()">
     <al-expand name="story-summary"/>
    </al-for>

Notice that all of the macros are referring directly to the `story` object which contains all of the data which pertains to one story.

If you find that your macros are referring to application data by name then you should consider using a function instead. Once functions have been implemented you might be able to use them as well as consider them.