vi studio

Lessons · Building apps · 19 of 36

Repeating things

repeat: task in tasks, grids, index, x, y and .at().

repeat: makes copies of a container.

main home:
  children:
    tiles

tiles:
  size: max, 30
  arrange: row, spread
  space_inside: 10
  children:
    tile

tile:
  repeat: 3
  size: 20
  color: yellow
  shape: rounded
tasks = ["Buy milk", "Call Sam", "Water the plants"]

main home:
  space_inside: 20
  space_between: 8
  children:
    task_row
    add:
      button: "Add a task"
      on_tap: tasks.add("Task {tasks.count + 1}")

task_row:
  repeat: task in tasks
  size: max, auto
  space_inside: 12
  color: gray[100]
  shape: rounded
  text: "{index + 1}. {task}"
  on_hold: tasks.remove(task)
main board:
  arrange: center
  children:
    grid:
      size: max, auto
      arrange: row, center
      children:
        cell

cell:
  repeat: 3, 3
  size: 30
  space_around: 2
  shape: rounded
  color: blue[200] if (x == y) else blue[400]
  arrange: center
  text: "{x}, {y}"
  on_tap: cell.at(x, y).color = orange[400]

Reach one copy with .at(): cell.at(1, 1) in a grid, task_row.at(4) in a list.

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.