ifdef and ifndef Directives
ifdef directive
Content between the ifdef
and endif
directives gets included if the specified attribute is set:
ifdef::env-github[]
This content is for GitHub only.
endif::[]
The syntax of the start directive is ifdef::<attribute>[]
, where <attribute>
is the name of an attribute.
Keep in mind that the content is not limited to a single line.
You can have any amount of content between the ifdef
and endif
directives.
If you have a large amount of content inside the ifdef
directive, you may find it more readable to use the long-form version of the directive, in which the attribute (aka condition) is referenced again in the endif
directive.
ifdef::env-github[]
This content is for GitHub only.
So much content in this section, I'd get confused reading the source without the closing `ifdef` directive.
It isn't necessary for short blocks, but if you are conditionally including a section it may be something worth considering.
Other readers reviewing your docs source code may go cross-eyed when reading your source docs if you don't.
endif::env-github[]
If you’re only dealing with a single line of text, you can put the content directly inside the square brackets and drop the endif
directive.
ifdef::revnumber[This document has a version number of {revnumber}.]
The single-line block above is equivalent to this formal ifdef
directive:
ifdef::revnumber[]
This document has a version number of {revnumber}.
endif::[]
ifndef directive
ifndef
is the logical opposite of ifdef
.
Content between ifndef
and endif
gets included only if the specified attribute is not set:
ifndef::env-github[]
This content is not shown on GitHub.
endif::[]
The syntax of the start directive is ifndef::<attribute>[]
, where <attribute>
is the name of an attribute.
The ifndef
directive supports the same single-line and long-form variants as ifdef
.
Checking multiple attributes
Both the ifdef
and ifndef
directives accept multiple attribute names.
The combinator can be “and” or “or”.
- Any attribute (or)
-
Multiple comma-separated (
,
) directive names evaluate to true, allowing the content to be included, if one or more of the directives is defined. Otherwise the content is not included.Example 5. Any attribute exampleifdef::backend-html5,backend-docbook5[Only shown when converting to HTML5 or DocBook 5.]
- All attributes (and)
-
Multiple plus-separated (
+
) directive names evaluate to true, allowing the content to be included, if all of the directives are defined. Otherwise the content is not included.Example 6. All attributes exampleifdef::env-github+backend-html5[Only shown when converting to HTML5 on GitHub.]
The ifndef
directive negates the results of the expression.