Lessons · Building apps · 19 of 36
Repeating things
repeat: task in tasks, grids, index, x, y and .at().
repeat: makes copies of a container.
repeat: task in tasksmakes one copy per item.taskis that copy's item, and the copies follow the list: add an item and a copy appears.repeat: 5makes five copies;repeat: 3, 3makes a grid of 3 columns and 3 rows.- In a copy,
indexis its place (from 0). In a grid,xandyare its column and row. - Containers written inside a copy see that copy's item,
index,xandytoo. - The copies belong to the parent, like children written out one by one.
repeat: 3is the same as listing the container three times: the parent'sarrangelines them up and itsspace_betweenis the gap. A grid is one block in the parent; the parent'sspace_betweenis the gap between its cells, and withspreadoreventhe grid fills the parent and spreads them.
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
- Screensgo_to, go_back, transitions, and phone / tablet / desktop.
- Containers that take valuescard(task): one container, many values.
- Reaching other containersInside names, parent.name and top-level names.
- Changing the screenA dot for one change, a block for several; wait and animate.
- Timers and gamesevery(), stop(), on_touch, place: x, y and on_hit.
- Sound and videoplay, loop, stop_sounds, audio and video.
- Files and projectssrc/, every file sees every name, one name in one file.
- The config blockApp name, store id, version, icon and splash screen.
- Errors and good habitsThe real error messages and their fixes; how to organise a project.
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.