Rock U - Lava - Assign Statement
Transcribed Video Content
As you start to build some more complex lava statements, you'll notice that you wanna start storing some values. So whether it's something dynamic that's been calculated or a string or a number that you wanna be able to use in the future, within your template, you should start using the assigned statement. I'll show you how easy it is and some ways to use it in Lava right now. Here's a great example of when I'd use the assign tag in Rock. So we have this really simple syntax, looks most other commands in Rock, then we type assign and then we pick the name of our assigned variable and then what we're assigning.
So in this case, it's something dynamic. We're simply getting the current hour here. So obviously that will update depending on the time. And just so , right now it's eleven and what that means is we expect this if statement to return good morning. So let's see if that's true.
And it's true. We got good morning. And just to show you what this assignment looks , we just have a number here. So let's say hour and you should be able to see the exact hour that we have added here. So boom, exactly.
It's 11AM right now and we can see that hour 11 is output. Now an example of why we would use this assign tag is, say you wanted to do an if statement and you wanted to pass something in this, it actually wouldn't work. You need to assign this variable since it's dynamic outside of this actual if command or statement. And I'll show you that this doesn't work. We'll click save and it says invalid if tag, but you can get the same result by passing in an assigned variable.
And to show you that you can also change this, we'll set this to an hour in the day that actually isn't possible. So hour thirty, and you can see we get this output here. So it's helpful to use the assign tag when you want to have a dynamic variable or say you have a statement with a specific variable passed in a bunch of different times and you wanna be able to easily change it in one place. This can really help you clean up your lava. On top of the flexibility that this statement offers you, it really just makes your lava easier to read and maintain.
So start using this statement more. I hope that you see its power in Rock and thank you for watching this video.