Glyph - Conditional Macros

Sometimes you may want text to be included in a document only if certain conditions are satisfied. For example, you may want to display a disclaimer section only if the document is a draft (see the document.draft setting), or use a particular stylesheet only if when you generate a PDF document.

To do so, you can use the condition macro (aliased by ?), and a set of additional macros that can be used as conditional operators i.e.:

Consider the following code:

1?[$[document.draft]|
2This is a first draft of the Glyph Book|
3This is the official version of the Glyph Book]

In this case, if document.draft is set to true, “This is a first draft of the Glyph Book” will be displayed; if not, “This is the official version of the Glyph Book” will be displayed instead.

The condition macro takes up to three parameters:

  1. the condition to evaluate
  2. the text to include in the document only if the condition is satisfied.
  3. (Optional) the text to include in the document if the condition is not satisfied.

Note that all parameters can contain macros, of course, so you can write things like:

1?[and[
2    eq[$[document.output]|pdf]
3    |
4    eq[$[tools.pdf_generator]|prince]
5    ]
6  |
7  style[pagination.css]]

In this case, the pagination.css stylesheet is included only when you're generating a PDF document using Prince XML.

Results of conditional expressions

The condition macro in Glyph works in a similar way as conditionals in programming languages: if the conditional expression (supplied as first parameter) is satisfied then the second parameter is executed or displayed. But when is a conditional expression satisfied? Glyph is a simple mini-language to perform text manipulation, and has no types, it can only understand text, therefore:

  • A conditional expression is satisfied if it evaluates to a non-empty string except “false”.
  • A conditional expression is not satisfied if it evaluates to an empty string or the string “false”.