vi studio

Lessons · Building apps · 18 of 36

Containers that take values

card(task): one container, many values.

A container can take values, like a function: write the names in brackets after its name, then use them in its attributes. Show it with values: task_card(tasks[0]).

tasks = [{ name: "Buy milk", done: true }, { name: "Call Sam", done: false }]

main home:
  space_inside: 20
  space_between: 10
  children:
    task_card(tasks[0])
    task_card(tasks[1])

task_card(task):
  size: max, auto
  space_inside: 14
  shape: rounded
  color: green[100] if (task.done) else white
  shadow: true
  text: task.name
  text_style: strikethrough if (task.done) else normal
  on_tap: task.done = not task.done

Containers written inside it see the values too, however deep:

main home:
  children:
    tab("🧳", "Trips")
    tab("🗺️", "Map")

tab(emoji, label):
  arrange: row
  space_between: 6
  children:
    tab_icon:
      text: emoji
    tab_name:
      text: label
      text_style: 12, bold

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.

More in Building apps

About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.