@if

Keeps its body when its condition holds, and its detail, if it has one, when it fails.

On this page

#Attributes

when
The condition, an expression without braces: audience = teacher, n >= 3 and {{show-value}}, not answers. It takes <, <=, >, >=, = and !=, which compare loosely (Blue = blue, 1.0 = 1), == and !==, which compare exactly, and, or, not, x in 0..5 (a < beside the dots leaves that end out: x in 0..<5, x in 0<..), and true and false.

#Allowed content

Body { }The content to keep when the condition holds — any Chalk that is legal where the @if is written

#Examples

@let(audience: teacher)

@if(when: audience = teacher){
    note{
        Answers are collected in the closing scene.
    }
}[
    note{
        Answer each question before moving on.
    }
]
@let(answers)

@if(answers){
    The answers follow each question.
}

#Notes

  • Beside = a word nothing declares is a name, kept as written and possibly several words: audience = teacher, country = new zealand; quoted, "new zealand", it is text and nothing else. Two texts compare as text, so "1.0" = "1" fails.
  • A bare key tests for true and !key for false: @if(answers) is when: answers, @if(!answers) is when: not answers, and several AND together.
  • Inside a condition a hyphen is a minus, so a hyphenated constant is written in braces: @if(when: {{show-value}} and n > 2).
  • A condition is required: @if: requires a condition — @if(when: …), or a bare key / !key.
  • A key: value pair is refused with the fix: @if: audience: teacher is not an attribute of @if — write @if(when: audience = teacher).
  • A condition gives a boolean, so a number is refused: @if: k + 1 gives a number where a boolean is wanted — compare it, as in k + 1 > 0.
  • The constant is declared above the @if. A name nothing declares is the word itself: @if(when: k > 3) is @if: k is not a valid number. A template's attributes and a @for binding, including a row's cells, count as constants.
  • A condition is decided as the document compiles, so one that reads a live value is refused: @if is decided while the document compiles and cannot depend on #f.slope.

Last updated