Conditional content¶
Three directives include their body only when a condition holds:
only asks which build is running, by its tags;
if asks a question of your variant data;
and choose asks one such question once and takes the first branch that holds.
A build leaves out what is excluded,
while the editor preview keeps it visible, collapsed and greyed, so you can see it exists.
A fourth, raw, passes its body to one output format only, untouched.
Variant conditions (if)¶
if takes a condition on your variant data,
read through the var name.
This site’s variant data sets stage to review,
so the first block below is shown and the second is left out.
Variant conditions
.. if:: var.stage == "review"
Shown while the ``stage`` is ``review``.
.. if:: var.stage == "published"
Left out until the ``stage`` is ``published``.
Shown while the stage is review.
```{if} var.stage == "review"
Shown while the `stage` is `review`.
```
```{if} var.stage == "published"
Left out until the `stage` is `published`.
```
The MyST form renders live in the MyST variant conditions.
The body can be any content, needs included:
a need inside a false if is not created.
What the preview shows for each, and what a condition may say, are in
The if directive.
One branch of several (choose)¶
choose holds when branches, each with a condition,
and an optional otherwise last.
It includes the first when whose condition holds,
or the otherwise when none does.
One branch of several
.. choose::
.. when:: var.stage == "draft"
Drafting: nothing is reviewed yet.
.. when:: var.stage == "review"
In review: this branch is the one taken.
.. otherwise::
Published.
In review: this branch is the one taken.
````{choose}
```{when} var.stage == "draft"
Drafting: nothing is reviewed yet.
```
```{when} var.stage == "review"
In review: this branch is the one taken.
```
```{otherwise}
Published.
```
````
The MyST form renders live in the MyST branches.
choose, when and otherwise come from Sphinx-Needs 9.0 and later.
What makes a choose invalid, and how the preview shows the branches not taken, are in
The choose, when and otherwise directives.
Raw output for one format (raw)¶
raw passes its body, unchanged and unchecked, to the output formats its argument names:
html reaches every HTML build, and nothing else.
Treat it as markup you are responsible for.
Raw HTML
.. raw:: html
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save the page.</p>
Press Ctrl + S to save the page.
```{raw} html
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save the page.</p>
```
The MyST form renders live in the MyST raw output.
A PDF is not an HTML build, so ubc build pdf prints nothing for this specimen,
as a Sphinx LaTeX build does.
The raw role, derived for a format with the role directive,
the :file: option, and the switch that turns raw content off,
are in Raw content.