Generating Output Conditionally

Here is a very simple example of generating output conditionally. Look in your m4 documentation for more examples of conditional m4 behavior.

We can define a macro debug like this:

define(`debug', `1')

Later, we can output html if the macro is defined:

ifdef(`debug', `Debug was defined.')

The ifdef macro allows an optional argument that gets output if the macro is not defined. Hence:

ifdef(`debug', `Debug was defined.', `Debug was NOT defined.')

You can also use the ifelse macro, which works in many ways. The simplest of these is something like

ifelse(`debug', `1', `Debug was one.', `Debug was NOT one.')

OK, so m4's a little weird. It only gets worse:

ifelse(`debug', `1', `Debug was one.', `debug', `2', `Debug was two.', `Debug was NOT one or two!')

And, believe it or not, you can use this (and practically nothing else) to define for loops and other goodies. Once again, look at your m4 documentation, or get the GNU docs. Or poke around in the html-macros file supplied with htm4l.

Back to the htm4l home page. Back to the htm4l examples page.
© Terry Jones (terry <AT> Last modified: Mon Oct 2 02:21:32 CEST 2006