Elements¶
XML-style Comments and Processing Directives¶
Ginger allows XML-style comments and processing directives but discards them. This is to ease pasting large chunks of XML, so you don’t need to go through by hand removing them:
<!-- XML comments are allowed -->
<p> "Thank goodness for that!" </p>
Printing as XML¶
The most important use of elements is to generate XML and XHTML output. You can send an element to a stream using print or println or convert it to a string using stringPrint.
N.B. When you are typing at the command line, elements (and all other values too) are shown rather than printed. So what you see on the command line is similar but different to how they will print.
Template Example¶
Here’s an example that turns a list into a table with a configurable border width:
define asTable( list, border_width ) =>>
<table border=(border_width)>
for x in list do
<tr><td> x </td></tr>
endfor
</table>
enddefine;
And here’s how it might be used:
<html>
<head> "Result List" </head>
<body>
computeResults().asTable( 1 )
</body>
</html>.println;
Functions for Building and Manipulating Elements¶
N.B. the function newElement is deliberately not documented at the time of writing since we’d like a chance to consider how the API is exposed before committing to it.
- isElement( OBJECT ) -> BOOL
Returns true if the OBJECT is an element, otherwise false.
- nameElement( E ) -> OBJECT
Return the name of the element. This is normally a symbol but could in fact be any value.
- for i in E do …. endfor
Iterates over the children of an element in turn.
- E[ N ] -> X
Same as index( N, E )
- index( N, E ) -> X
Returns the Nth child of an element.
- E.attribute( KEY ) -> VALUE
Returns the attribute of an element associated with a KEY. If not present then it return absent.
- length( E ) -> N
Returns the number of children of an element.