Differences between revisions 4 and 5
Revision 4 as of 2006-06-26 05:52:27
Size: 3003
Comment:
Revision 5 as of 2011-02-15 06:05:18
Size: 3003
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
Macro handling in standard Albatross is powerful but a bit unwieldy to use and quite verbose, especially when the macro arguments are small. So I have made two small patches to make this easy. The first is the ability in the <al-usearg> tag to specify a default value for the argument. The second is ["Simple Macro Args"]. Macro handling in standard Albatross is powerful but a bit unwieldy to use and quite verbose, especially when the macro arguments are small. So I have made two small patches to make this easy. The first is the ability in the <al-usearg> tag to specify a default value for the argument. The second is [[Simple_Macro_Args]].

Default Macro Args

Note that a variation on the functionality described here will appear in the next version of Albatross after 1.35 (AndrewMcNamara).

Macro handling in standard Albatross is powerful but a bit unwieldy to use and quite verbose, especially when the macro arguments are small. So I have made two small patches to make this easy. The first is the ability in the <al-usearg> tag to specify a default value for the argument. The second is Simple_Macro_Args.

The default arg is used if a matching <al-setarg> is not specified in the <al-expand>. Unlike real macro arguments, the default arg must be a simple string; using <al-value> etc in a default argument is not supported (tho it could be with not too much difficulty).

We use the default arg in cases where you are specifying simple formatting (colors, styles etc) and may want to override them. Like this:

<al-macro name="box">
  <table border="0" cellpadding="1" cellspacing="0">
      <tr><td valign="top"  bgcolor="<al-usearg name="color" default="gray">">
          <table align="center" border="0" width="100%" cellpadding="5">
              <tr><td valign="top" bgcolor="white"><al-usearg></td></tr>
  </table>
  </td></tr>
  </table>
</al-macro>

This macro takes the unnamed macro arg and formats it into a box with the specified color (which defaults to "gray"). You might use it like this:

<al-expand name="box">
    <table>
        # ....
    </table>
</al-expand>

for a grey box, or

<al-expand name="box">
  <al-setarg name="color">green</al-setarg>
  # etc

for a green box.

The patch (against 1.10) is as follows:

--- albatross/tags.py.DIST      Mon Jul 21 12:40:16 2003
+++ albatross/tags.py   Mon Jul 21 12:45:47 2003
@@ -1154,7 +1154,15 @@
     name = 'al-usearg'
 
     def to_html(self, ctx):
-        content = ctx.get_macro_arg(self.get_attrib('name'))
+       try:
+            content = ctx.get_macro_arg(self.get_attrib('name'))
+       except ApplicationError:
+           # OK, look for a default= clause
+           if self.has_attrib('default'):
+               ctx.write_content(self.get_attrib('default'))
+               return
+           else:
+               raise
         # Peel one level of arguments off stack to evaluate enclosed
         # content then restore the stack afterwards.  Nested macros
         # cause infinite recursion otherwise.

(beware that cut-n-pasting this will probably mess with whitespace so you may need to apply the patch by hand, or use "patch -l" or some such.)


Another possibility for defining a macro with default args could be:

<al-macro name="box" color="gray">
    ...
    <al-usearg name="color" />
</al-macro>

or even the following if it's easier for the macro parsing:

<al-macro name="box" args="{'color':'gray'}">
</al-macro>

I don't like the look of that second version much though.

- Matt

None: Default_Macro_Args (last edited 2011-02-15 06:05:18 by localhost)