sercrod

*default

Summary

*default is the fallback branch for *switch when no *case matches. It participates in a JavaScript-like switch/fall-through model:

*default has an alias n-default with the same behavior.

Basic example

A simple switch with a default branch:

<serc-rod id="app" data='{"status":"idle"}'>
  <div *switch="status">
    <p *case="'ready'">Ready</p>
    <p *case="'error'">An error occurred</p>
    <p *default>Unknown status</p>
  </div>
</serc-rod>

Behavior:

Behavior

*default marks an element as part of the default region of a *switch block:

Alias:

Evaluation timing

*default is evaluated as part of the *switch pass, not as an independent directive:

Outside of a *switch:

Execution model

The *switch execution model (simplified):

  1. On the host element that has *switch or n-switch:

    • Evaluate the switch expression in the current scope to produce switchVal.
    • Prepare a childScope that includes the original scope plus $switch: switchVal.
  2. Walk each direct child element of the *switch host in DOM order:

    • Compute two flags for each child:
      • isDefault if it has *default or n-default.
      • caseRaw if it has any of *case, n-case, *case.break, or n-case.break.
    • If neither isDefault nor caseRaw is present, the child is not a switch branch and is skipped at this stage.
  3. Before rendering starts (falling === false):

    • If isDefault is true:
      • Start rendering from this branch: set falling = true.
    • Else if there is a caseRaw:
      • Use the internal _matchCase helper to compare the case expression with switchVal.
      • If it matches, start rendering from this branch: set falling = true.
      • If it does not match, skip this child and continue scanning.
  4. Once rendering has started (falling === true):

    • Clone the branch element.
    • Remove control attributes from the clone:
      • *case, n-case
      • *default, n-default
      • *case.break, n-case.break
      • *break, n-break
    • Render the clone normally with childScope as the scope and append it to the parent.
  5. Break handling:

    • After rendering a branch, determine if it has any break-related attributes:
      • *break, n-break
      • *case.break, n-case.break
    • If any of these are present on the original branch, stop processing further branches in this *switch block.

Notes about *default in this model:

Variable creation and scope layering

*default does not introduce any new variables by itself.

Inside a *default branch:

Typical usage:

<div *switch="status">
  <p *case="'ready'">Ready</p>
  <p *case="'error'">Error: <span *print="$switch"></span></p>
  <p *default>Unknown status "<span *print="$switch"></span>"</p>
</div>

Here, the default branch reports the unknown value by reading $switch from the child scope.

Parent access

*default does not change the way parent data is accessed:

In other words, *default only affects where rendering starts and how branches fall through; it does not introduce a new “parent” concept.

Use with conditionals and loops

Inside a *default branch you can freely use other directives:

Use with *case, *case.break, and *break

*default participates in the same fall-through model as *case and *case.break:

Sercrod-specific constraints and recommendations:

Best practices

Additional examples

Default with a detailed fallback view:

<serc-rod id="dashboard" data='{
  "mode": "unknown",
  "knownModes": ["list","detail"]
}'>
  <section *switch="mode">
    <div *case="'list'">
      <h2>List view</h2>
      <!-- ... -->
    </div>

    <div *case="'detail'">
      <h2>Detail view</h2>
      <!-- ... -->
    </div>

    <div *default>
      <h2>Unsupported mode</h2>
      <p>
        Mode "<span *print="$switch"></span>" is not supported.
      </p>
      <p>Supported modes are:</p>
      <ul *each="m of knownModes">
        <li *print="m"></li>
      </ul>
    </div>
  </section>
</serc-rod>

Default plus break to isolate fallback:

<div *switch="status">
  <p *case="'ok'" *break>OK</p>

  <p *default *break>
    Status "<span *print="$switch"></span>" is not OK
  </p>

  <p>
    This paragraph is never rendered, because all branches break.
  </p>
</div>

Notes