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”.
The two combinators cannot be combined in the same expression.
ifdef with multiple attributes
- If any attribute is set (or)
-
Multiple attribute names must be separated by commas (
,
). If one or more of the attributes are set, the content is included. Otherwise, the content is not included.Example 5. If any attribute exampleifdef::backend-html5,backend-docbook5[Only shown if converting to HTML (backend-html5 is set) or DocBook (backend-docbook5 is set).]
- If all attributes are set (and)
-
Multiple attribute names must be separated by pluses (
+
). If all the attributes are set, the content is included. Otherwise, the content is not included.Example 6. If all attributes exampleifdef::backend-html5+env-github[Only shown when converting to HTML (backend-html5 is set) on GitHub (env-github is set).]
ifndef with multiple attributes
The ifndef
directive negates the results of the expression.
When using the ifndef
directive, the expression should be read with the prefix “unless”.
- Unless any attribute is set (or)
-
Multiple attribute names must be separated by commas (
,
). If one or more of the attributes are set, the content is not included. Otherwise, the content is included.Example 7. Unless any attribute exampleifndef::profile-production,env-site[Not shown if profile-production or env-site is set.]
- Unless all attributes are set (and)
-
Multiple attribute names must be separated by pluses (
+
). If all of the attributes are set, the content is not included. Otherwise, the content is included.Example 8. Unless all attributes exampleifndef::profile-staging+env-site[Not shown if profile-staging and env-site are set.]