|
| 1 | +.. _if: |
| 2 | + |
| 3 | +if |
| 4 | +== |
| 5 | + |
| 6 | +.. versionadded:: 8.2.0 |
| 7 | + |
| 8 | +The ``if`` directive conditionally includes or excludes content based on |
| 9 | +:ref:`variant data <filter_variant_data>` evaluated at parse time. |
| 10 | + |
| 11 | +The directive argument is a Python expression evaluated against the ``var`` |
| 12 | +namespace (populated from :ref:`needs_variant_data`). |
| 13 | +If the expression evaluates to ``True``, the directive body is parsed and |
| 14 | +included in the document. Otherwise the entire body is skipped. |
| 15 | + |
| 16 | +.. code-block:: rst |
| 17 | +
|
| 18 | + .. if:: var.arch == "arm" |
| 19 | +
|
| 20 | + This paragraph only appears when ``needs_variant_data`` |
| 21 | + has ``arch`` set to ``"arm"``. |
| 22 | +
|
| 23 | + .. req:: ARM-specific requirement |
| 24 | + :id: REQ_ARM_001 |
| 25 | +
|
| 26 | + This need is only created for the ARM variant. |
| 27 | +
|
| 28 | +Usage |
| 29 | +----- |
| 30 | + |
| 31 | +Basic conditions |
| 32 | +~~~~~~~~~~~~~~~~ |
| 33 | + |
| 34 | +.. code-block:: rst |
| 35 | +
|
| 36 | + .. if:: var.debug |
| 37 | +
|
| 38 | + Debug-only content here. |
| 39 | +
|
| 40 | + .. if:: var.build.optimization > 1 |
| 41 | +
|
| 42 | + High-optimization content. |
| 43 | +
|
| 44 | +Membership tests |
| 45 | +~~~~~~~~~~~~~~~~ |
| 46 | + |
| 47 | +.. code-block:: rst |
| 48 | +
|
| 49 | + .. if:: "feature_x" in var.build.features |
| 50 | +
|
| 51 | + Feature X documentation. |
| 52 | +
|
| 53 | +Nested if directives |
| 54 | +~~~~~~~~~~~~~~~~~~~~ |
| 55 | + |
| 56 | +``if`` directives can be nested: |
| 57 | + |
| 58 | +.. code-block:: rst |
| 59 | +
|
| 60 | + .. if:: var.arch == "arm" |
| 61 | +
|
| 62 | + .. if:: var.debug |
| 63 | +
|
| 64 | + ARM debug-specific content. |
| 65 | +
|
| 66 | +Content with sections |
| 67 | +~~~~~~~~~~~~~~~~~~~~~ |
| 68 | + |
| 69 | +The body may contain section headers and any valid reStructuredText: |
| 70 | + |
| 71 | +.. code-block:: rst |
| 72 | +
|
| 73 | + .. if:: var.arch == "arm" |
| 74 | +
|
| 75 | + ARM-specific section |
| 76 | + ~~~~~~~~~~~~~~~~~~~~ |
| 77 | +
|
| 78 | + Content under a conditional heading. |
| 79 | +
|
| 80 | +Expression context |
| 81 | +------------------ |
| 82 | + |
| 83 | +Only the ``var`` namespace is available in the expression. |
| 84 | +Built-in Python functions (``open``, ``import``, etc.) are **not** accessible. |
| 85 | + |
| 86 | +If the expression references a variant key that does not exist, a warning is |
| 87 | +emitted and the content is skipped. |
| 88 | + |
| 89 | +Supported operators: |
| 90 | + |
| 91 | +- Comparison: ``==``, ``!=``, ``<``, ``>``, ``<=``, ``>=`` |
| 92 | +- Logical: ``and``, ``or``, ``not`` |
| 93 | +- Membership: ``in`` |
| 94 | +- Attribute access: ``var.a.b.c`` (nested dicts) |
| 95 | + |
| 96 | +Behavior |
| 97 | +-------- |
| 98 | + |
| 99 | +- **Parse-time evaluation**: The condition is evaluated during RST parsing. |
| 100 | + Unlike Sphinx's ``only`` directive (which defers to doctree resolution), |
| 101 | + excluded content is never parsed at all. |
| 102 | +- **No need data access**: Because parsing happens before all needs are |
| 103 | + collected, need fields and IDs are not available in the expression. |
| 104 | + Use :ref:`filter` for need-aware filtering. |
| 105 | +- **Incremental builds**: If a document is re-read (e.g., because the source |
| 106 | + changed), all ``if`` directives in it are re-evaluated. |
| 107 | + |
| 108 | +Warnings |
| 109 | +-------- |
| 110 | + |
| 111 | +The directive emits warnings (suppressible via ``suppress_warnings = ["needs.if"]``) when: |
| 112 | + |
| 113 | +- ``needs_variant_data`` is not configured but the directive is used. |
| 114 | +- The expression raises an exception (syntax error, unknown key, etc.). |
0 commit comments