Rock U - Lava - If Statements
Transcribed Video Content
If statements are crucial for logic in Lava, and that's because when you're working with data, sometimes you wanna pick when something is shown or when something happens or when it doesn't happen. So for example, if you wanted to return some data on people in the church and you wanted to make sure that you don't return any data from people that haven't attended in the last few months, then you would use something an if statement to limit what you get back. Let's look at all the different ways you can use if statements in Rock and we'll use some simple examples so you can understand the concept. Let's get into it right now. So I'm looking at the documentation for the if tag, which is on our community website.
And again, this tag is super important and it's very simple to use. So I'll show you what it looks . First of all, you'll have a tag that you put up top where you say if and you'll have a condition, then you'll close that tag off. And between that, you'll put what gets output if this condition is met. Then continuing, you'll put a second tag if you need a second tag saying else if, and this is only if you go over a certain number of conditions, but you'd put in an else if tag.
And again, you can put in those conditions and just wanna point out this right here, this equal sign is an operator. We have a bunch of different operators you can use so that you can specify conditions, but this is the second condition, we've closed it. And then if this condition is met, we'll show this off. Then you can say else if you want to have a result that pops up, if no other condition is met, Then you'll put in the end if tag. So there's all sorts of different ways you can do if conditions, and these are really important because there are lots of times where you don't want certain things to happen or you do want certain things to happen based on a couple different conditions.
Now I wanna go over the operators real quick. First of all, the most simple one that you'll probably use pretty often is an equals operator. There's also not equal. Note that the equal sign always comes last in these operators. You can also do greater than, less than if you have something numbers that you want to have as conditions.
You can do greater than or equal, less than or equal. You can do an or if you want multiple things to be part of conditions such as condition a or condition b or if you want to have just one of these be the case, one condition, you can use the and operator or contains, which is really nice if you're using a string, say in the results only part of the string needs to be matched for that condition to be met, then you can say contains. Now there's a bunch of other things that you can do with these if tags. There's a bunch of examples in our documentation, but let's look at some real examples. So this is a very simple one.
We're simply doing an if the current person has a connection status value ID that equals one, then say welcome member. Else if the current person has a connection status value ID equals two or it equals three, meaning either of these conditions can be met, then we'll say welcome current person dot nickname. And if none of those conditions are met, just say welcome. And the actual value I have right now doesn't match one, two, or three, meaning we just got the output that says welcome. Now here's a more complex example, and this is actually showing something that doesn't work.
You'll occasionally be tempted to do something use a filter within an if. Now filters don't work in an if statement, but I'll show you a workaround for it in just a second. In this case, we're trying to say if the current hour is something, then good morning. Or if the hour is less than eighteen within the day, say good afternoon. Otherwise, say good evening.
But the output for this would actually be just a straight up error saying that this doesn't work. Now this is the correct way to do it. You can pass in the results of a filter by simply using an assign statement. Here, we're assigning this variable hour as the current hour, so it'll just pass as this simple number. For example, right now, as I'm recording this, it's afternoon time, meaning that the hour is, more than eighteen in this case or actually, sorry, less than eighteen.
So the condition is being met that says good afternoon, and that's the output that we get. Great. Now you have conditional logic in your Lava templates. This will allow you to really expand what you can use Lava for and be specific about what you want to happen. Thanks for watching this video and have a great day.