[albatross-users] Macro default args

Gregory Bond gnb at itga.com.au
Wed Jan 15 14:25:15 EST 2003


I needed this so I made the attached patch (code only, not docco!)

Usage:
	<al-usearg name="name" default="default text.....">

This is OK for small amounts of text, but not fabulous for large macro args.  
I use it for small things like specifying colors etc:

<al-macro name="box">
  <table>
      <tr><td bgcolor="<al-usearg name="color" default="gray">">
	  <table width="100%">
	      <tr><td bgcolor="white"><al-usearg></td></tr>
  </table>
  </td></tr>
  </table>
</al-macro>

<al-expand name="box">Normal stuff in a box</al-expand>
<al-expand name="box"><al-setarg name="color">red</al-setarg>Error!!</al-expand>


-------------- next part --------------
--- albatross/tags.py.DIST	Thu Sep  5 00:07:10 2002
+++ albatross/tags.py	Wed Jan 15 14:17:50 2003
@@ -9,6 +9,7 @@
 import time
 
 from albatross.template import Content, EmptyTag, EnclosingTag
+from albatross.context import MacroError
 
 # Empty content object which tags can use as default value for
 # optional child tag content.  <al-head>, <al-tail>, etc.
@@ -1054,7 +1055,14 @@
     name = 'al-usearg'
 
     def to_html(self, ctx):
-        text = ctx.get_macro_arg(self.get_attrib('name'))
+    	try:
+            text = ctx.get_macro_arg(self.get_attrib('name'))
+	except MacroError:
+	    # OK, look for a default= clause
+	    if self.has_attrib('default'):
+	    	text = self.get_attrib('default')
+	    else:
+	    	raise
         ctx.write_content(text)
 
 class Macro(EnclosingTag):


More information about the Albatross-users mailing list