Template Processing Classes for Python
by Brad Howes


Example 1:

>>> from BoilerPlate import String
>>> z = '''a
#if b#
b
#else#
c
#endif#'''
>>> print String( z, lineMode = 0, b = 1 )
a
b
>>> print String( z, lineMode = 1, b = 1 )
ab
>>> print String( z, lineMode = 1, b = 0 )
ac


Example 2:

>>> from BoilerPlate import String
>>> z = 'A#if a == 1#B#elif a == 2#C#else#D#endif#E'
>>> print String( z, a = 1 )
ABE
>>> print String( z, a = 2 )
ACE
>>> print String( z, a = 99 )
ADE


Example 3:

>>> from BoilerPlate import String
>>> z = '''#for x in a#%(x.index)d %(x.value)d
#end for#Total: %(x.sum)d'''
>>> print String( z, a = range( 0, 100, 10 ) )
0 0
1 10
2 20
3 30
4 40
5 50
6 60
7 70
8 80
9 90
Total: 450

>>> from BoilerPlate import String
>>> z = 'Page %( HtmlEncode( '<' + Lower( Roman( foo ) ) + '>' ) )s'
>>> print String( z, foo = 14 )
Page &lt;xiv&gt;
>>>


Listing One
print "<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY>" % title
# Trap when there is no data to show.
if len( data ) == 0:
    print "<B>No data available</B><P>"
else:
    # Print table heading, then each row
    print "<TABLE><TR><TH>Index</TH><TH>Value</TH></TR>"
    for index in range( 0, len( data ) ):
        print "<TR><TD>%d</TD>" % index
        print "<TD>%s</TD></TR>" % data[ index ]
    print "</TABLE>"
print "</BODY></HTML>"

Listing Two
<HTML>
<!-- Example of BoilerPlate HTML -->
<HEAD>
<TITLE>%(title)s</TITLE>
</HEAD>
<BODY>
<!--
  -- Trap when there is no data
  -->
#if len( data ) == 0#
    <B>No data available</B><P>
#else#
    <!--
      -- Print table heading, then each row
      -->
    <TABLE>
    <TR><TH>Index</TH><TH>Value</TH></TR>
    #for each in data#
        <TR><TD>%(each.counter)d</TD><TD>%(each.value)s</TD></TR>
    #end for#
    <TR><TH>Total:</TH><TD>%(each.sum)</TD></TR>
#end if#
</BODY>
</HTML>


Listing Three
# Cook -- apply the given dictionary to a range of text we own.
def Cook( self, sink, dict ):
    sink.Append( self.text % dict )

1


