Rock U - Lava - For Statement

Transcribed Video Content

When you receive data using Lava or SQL, you often don't just have one item. Often, you're getting data back that is a list of items, And the for statement allows you to loop through the data you get back and you can pick what the shape of that data is essentially and then you can work with that data. So let's look at how to set up the for statement and how to use it in Rock right now. So now I'm on the Lava documentation, which is a super helpful resource. I always come back here from working on Lava as it's really helpful to know what parameters are available to you and remind yourself of what the syntax looks . But we're looking at the for tag and it's a really simple way to take an array or some information we have and kind of output the results. So it's really simple. We have this syntax. We simply open with this for tag and then you pick an alias. And this can be whatever you want, but it's helpful to keep it simple just based off of the array you're looping through. So we're looking at... We're iterating through campuses within the campuses array. So this is what it should look . And then you can use that alias within the actual for tag to access the information that you wanna output. So in this case, say there's 30 campuses and we're outputting campus dot name, this for loop will just keep going until it's output every single campus. Then you'll type int for. And because this is a loop, it'll keep running. There are some helpful ways to limit what comes back or change it. For example, you can use you can use things such as reversed to change the order. You can limit the number of results or even offset, meaning you skip some of the results. But let's look at a real example based off of this right now. So here is some real lava. But first of all, we want to look at the array. So this is the data that we have. We're simply showing campuses, and we have a name, main campus, south inactive, and north campus. And this is real data we have in our database. So right here, we have a list. We're simply using a list in HTML so that this looks pretty, But we're using this for tag to iterate through the array with the alias we've picked and it will just keep going looping through all these things, placing them within our HTML structure. And then this is what the actual output looks . So based off the array, this makes total sense. We simply output the campus name for our list of campuses. Okay. The limits of what you can do with Lava really just changed because being able to work with multiple pieces of data really expands how powerful Lava is. So I hope you have a good understanding now of what the for statement is and how you can use it in Rock. Have a great day.