brumate

Control flow

Blocks start with #, such as #if and #for, and wrap their body in braces. A block's code ships to the browser only if what it depends on can change there. Otherwise the block is HTML only.

#if

#if (condition) renders its body when the condition is true. #else if and #else follow it:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#for

#for (item of list) renders its body for each item. #else renders when the list is empty:

App.brmEdit the code to run it again

The sample runs when it comes into view.

Pick a key that stays the same for an item, such as an ID from a database. A key that changes renders the item again.

Windowed lists

A long list can render only the items in view. window(height) after the header gives the height of an item in pixels, and the browser renders the items that come into view as the user scrolls:

<div style="height: 400px; overflow: auto">
  <table>
    <tbody>
      #for (row of rows by row.id) window(32) {
        <tr><td>{row.name}</td></tr>
      }
    </tbody>
  </table>
</div>

A script in a block

A block body can start with a <script>. It runs for each instance of the body, such as for each item of a #for, and can declare signals and functions of its own:

App.brmEdit the code to run it again

The sample runs when it comes into view.

Each question has its own open. The script can read the item and the values of the component.

#repeat

#repeat (index of count) renders its body count times, with index from 0. #else renders when the count is 0:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#switch

#switch renders the first #case whose condition is true, or #default:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#key

#key (value) renders its body again when the value changes. The state inside the body starts over, such as the signals of a block script or of a component:

App.brmEdit the code to run it again

The sample runs when it comes into view.

Type a note, then switch: the field empties, because the body renders again for the new contact.

#template

#template name(parameters) declares a piece of markup that takes arguments. Call it in an expression:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#try and #catch

#try renders its body, and #catch renders instead if the body throws. #catch (error, reset) names the error and a function that renders the body again:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#portal

#portal renders its body somewhere else in the page, such as a dialog at the end of the body. #portal (target) renders into an element or the element that a selector finds:

App.brmEdit the code to run it again

The sample runs when it comes into view.

#offscreen

#offscreen (visible) shows its body while visible is true. When it turns false, the body is hidden but stays in the page, with its elements and its state:

App.brmEdit the code to run it again

The sample runs when it comes into view.

Type a note, open the reply and come back: the note is still there.

Use it for tabs, steps of a form, or panels that the user switches between. Use #if when the body should start over each time it shows.

#client

#client renders its body in the browser only. See #client blocks.

#loading

#loading shows a fallback while the values that its body reads load. See #loading.

#defer

#defer renders its body on the server later, in a request of its own. See #defer.

#head renders its body into the <head> of the page. See #head.