About Installation Guide Data Views Components Examples


Moon views are defined using the Moon View Language. The Moon view language is a template language based on HTML and adds support for data interpolation, events, and components.


Properties from instance data can be interpolated into the view using a pair of curly braces: {}.

<div class={name}>
    root: "#root",
    name: "interpolation",
    message: "Hello Moon!"

Try entering ViewInterpolation.update("message", "New Message!") in the console to update the view.


Browser events, component events, and lifecycle events can be handled by using an attribute starting with @.

Browser Events

Browser events such as click or input can be handled by creating an attribute prepended with @ with one statement as a value. A special local variable named $event will be available and holds the event object.

<button @click={announce($event)}>
    Make Announcement
    root: "#root",
    announce($event) {
        alert($ + " was clicked.");
Bind Events

The bind event is a special event used to bind an input value to a variable and a variable to the input value.

<input type="text" @bind={text}/>
    root: "#root",
    text: "Hello Moon!"

Try entering ViewBindEvents.update("text", "New Text!") in the console to update the view.

Component Events

Learn about component events in the components section.


Elements can be rendered based on certain conditions using the If, ElseIf, and Else components.

<div If={condition}>
    Condition is truthy.
<div Else>
    Condition is falsy.
    root: "#root",
    condition: Math.random() <= 0.5 ? false : true

Try entering ViewConditionals.update("condition", !ViewConditionals.condition) in the console to update the view.


Elements can be rendered multiple times for every element in an array using the For component. The syntax for the main argument is $element,$index in array. The element and index are prefixed with $, meaning that they are locals and not on the instance itself.

    <li For={$item,$index in items}>
        {$index}: {$item}
    root: "#root",
    items: ["foo", "bar", "baz"]

Try entering ViewLoops.update("items", ViewLoops.items.reverse()) in the console to update the view.

Alternate Syntax

Since If, ElseIf, Else, and For are just components, you can use the normal component syntax.

    <p>Condition is truthy.</p>
    <p>Multiple elements are allowed.</p>
    <p>Condition is falsy.</p>

<For={$item,$index in items}>