RX 24 - Lava 101 - Courtney Cooksey

Transcribed Content

Good morning, everybody. Our first session of the day is gonna be Lava one zero one with Courtney Cooksey. Please give her a round of applause. Welcome. Hey everybody, so as he said, my name's Courtney Cooksey. I'm a software engineer and this year I've been living in Little Rock, Arkansas, but I actually work for The Crossing in Columbia, Missouri. Today we are gonna talk about what is Lava, how do you write lava, and where can you use lava, and there's more cute pictures of my dogs, Rex and Loki, because dogs are very cute. Today we're gonna start out just general high level what is lava. Then Lava is kind of separated into some different sections so we'll go over Lava tags, Lava filters, and Lava commands and then finally go over a couple resources that are really good. So first, just high level, what is Lava? Well usually when we have static websites, we have a text file on our web server that has all the code for the page, the HTML and it's hard coded and you get what you get. That text file is sent to your browser, it interprets it and displays it for the users but you have a universal experience. Everybody sees the same thing. With dynamic websites, we can start by asking a question like who's actually looking at the page right now? From there, we can say, okay, is this an unknown user or a logged in user? And based on that, now we can actually create that file dynamically and change the data a little bit. So if it's unknown, we can just say hello user but if it's somebody logged in, can actually use their name. So now when we send that to the browser, you've got a custom user experience. So this is great but how do we actually do that? Well, we have to use our different types of web languages. We've got markup languages like HTML that say, hey, this is the layout of the page, stylesheet languages like CSS that define how elements on the page should be displayed, and then finally we have our templating languages which is what Lava is and that allows us to define placeholders in our HTML files that our server can say, okay, this is what I'm supposed to customize. This is what we're gonna change out to give that dynamic website to change what's displayed. Alright, all the examples I'm gonna go over today, you can do either in like the Lava Tester or an HTML block. Once again, here's the QR code for the code examples. On my GitHub, I've made it really easy for you to be able to just copy each one and then paste it into that if you want to follow along. Basic Lava syntax, whenever we want to define a placeholder, we're going to use double curly braces in our HTML. So if the hard coded was something like hello user, we would say alright user is what we want to be a placeholder and wrap the curly braces around it. Well if you try that you'd be like cool but that didn't actually work and that's because your web server has no idea what user is supposed to be. It doesn't have any context for that. So we have this word called variables. So a variable is just an object that lives on the server and the contents or the value of this object can change based on work the server does or context we provide it. You're also going to hear the word entity thrown around a lot whenever you're talking about rock and it can be kind of confusing because we use entity to mean a couple different things. At its most basic, an entity is an object that has properties and attributes. So sometimes we are talking about an entity type really like person. Person is an entity in Rock and first name is a property of the person entity where something like allergy or school is an attribute of the person entity and we'll talk about why those are different in a second. The other way we use the word entity is also sometimes to mean like a specific record. So my person record in Rock is also an entity. So just know that sometimes you're gonna hear that word and it will mean one or the other, either person as a whole or a specific record. So why are property and attribute different? Well, we can kind of think of the rock database like an Excel workbook. The workbook as a whole is our database and then each new sheet within that workbook is a table in our database. Each row in that sheet is a record or an entity that we can retrieve and then each column is a property and this is where properties and attributes are different because the properties are the columns in the table and the attributes are stored somewhere else. So we have an example here of a few columns that are in our person table. Everybody always has an ID, a first name, a last name, an email, and a birth date. So that's kind of what it would look like in our database. So the variable that stores the current user on your Rock server is called currentPerson. Now depending on your version of Rock or what blocks you're using, sometimes just the word person is also available but for me we're on version 15 right now and I've had the most luck and consistency with using currentPerson but just know that sometimes either is available. We're gonna use the dot notation to access properties of our variables. So here you can see we've got our currentPerson variable dot nickname where nickname is a property on our person record and that's how when we wrap that in curly braces we're telling the server whoever's currently logged in, print their nickname. All Lava's gonna be defined by a set of matching symbols. So like we showed before, when we want to print something, we use the double curly braces to say this is something that should print to the page. But sometimes we want to do some work. We want the server to run some code and when we do that, we do a curly brace and a percent sign and that is the way to tell the server this shouldn't be printed on the page but you should execute this code. Now let's talk about Lava tags. What are tags? Well, it's just a keyword that allows us to add some additional logic. We can create custom variables. We can assign their values. We can handle scenarios that have conditional logic like if this, then do this. We can repeat a section of logic over and over without having to write that out every single time, and we can also define sections of code that shouldn't actually process any lava within it. Tags can either be inline or block. When it's something simple, some of our lava tags are inline so the format will look like curly brace percent, the name of the tag, and then sometimes we have parameters and values we need to also pass and then we just have the percent sign and the curly brace to close it out. That's what an inline tag looks like. We call it inline because it's just one line all there. Other times we need to do some more work. We have something more complex. We need to process some things. So in this case, we'll have a block tag. The opening tag for your block tag is gonna look very similar. Then we'll have something inside of it to process and then to close it out and say, hey, we're done with this, we'll just repeat the tag name but we'll put the word end in front of it. So then we just have end whatever the tag name is and that's how we tell the server we're done with this. The assign tag is used to create and set the value of custom variables. So this is an inline tag and you can see here we just have assign, the tag name, and then our parameter which is the variable name. Then we set it equals by just doing an equal sign and then we put the value. So in this case, we're creating the variable called my variable in the first line of this code. Then we're just printing it on the page then we're going to reassign it to the second value and then print my variable has been changed to and now this time it will say the second value instead of the first value. There's a lot of different types of values we can assign to our custom variables. So in programming, you'll hear us talk about strings. This is going to be letters, words, phrases, any text and usually in Lava we always wrap that in single quotes. I think in the new Lava engine though, you can also use double quotes when you have a single quote within your text that you're trying to assign, but for the older stuff, it's all gotta be single quotes. We can do numeric values, integers, decimals, decimals. We've got boolean, which is just a special programming value for a true false. So if all we need to know is just kind of a yes or no, then we'll use Boolean values. Then we also have dates. There's a lot of different formats we can save dates in. Typically what you'll find is if a date is saved in the Rack database, it's gonna be year, month, day, the letter t, and then hours, minutes, seconds. But there's a filter that will allow us to format that in different ways. The capture tag is very similar to the assign tag in that it's used to create and assign custom variables but the capture tag is a block tag and so that allows us to do something more complex. We don't just have one specific string or number that we need to assign. We need to do some work, we need to do some conditional logic and then save that to our variable. So here we've got capture as our tag name, then the variable name greeting, then inside of that everything between the capture and the end capture tag is what will be stored in our variable greeting. So in this case it would be hello and then injecting whoever the currently logged in person's nickname is. So if this were me looking at it, it would print hello comma Courtney. Now let's get into some conditional logic. We're gonna use an if tag. So if is going to allow us to set a condition and if that condition is met then we will process what is inside between the if and the end if tag. So in this case, our example here, we're saying if current person dot last name is equal to Decker, that is the only time we will execute what is inside that if and end if tag. So we've got a few different comparison operators we can use for if statements. Is the first term equal to the second term? In programming, whenever we want to assign a value, we use a single equal sign. So to check for equality, we have to use a double equal sign. You will probably at some point accidentally assign something instead of checking if they're equal. It happens to all of us, but that's what we need to do, so that double equal sign to get equality, and on the opposite, if we wanna say, is the first term anything except the second term, are they not equal? Then we use exclamation point equal sign because in programming an exclamation point means not. We can do mathematical comparisons. They work great on numbers obviously, also dates. You can even use them on text though and it's usually going to be an alphabetical comparison of what comes first. There's also a special one called contains and this is asking does the first term have part of it equal to the second term? So if we were to take an example of maybe looking at somebody's list of allergies, we might want to find everyone whose allergy list has the word peanut in it. We don't really care what else but if it's peanut we want to do something maybe mention something about a snack that's being provided. So that's where we could use a contains. So to use these, we've got if current person dot last name equals Decker, only print when the logged in user's last name is Decker. If current person dot last name not equal Decker, only prints when it's anything except Decker. If current person dot last name contains d, only print when the currently logged in user's last name has a capital d in it. And then lastly, if current person dot age is greater than or equal to 50, so only people who have an age of 50 or above. We can also have multiple conditions. So sometimes there's multiple things we need to check and to do that we can just chain them together in our if opening tag and if we want all conditions to be met, then we use the word and in between each of those terms and if it's something where any of the conditions are valid and should execute this code, we'll use or in between them. So in this example here, our first one, we're checking that their last name is equal to Decker and their first name is equal to Ted. If your name's not Ted Decker, it's not gonna execute or print for you. In our second example, we're checking if last name is equal to Decker or last name is equal to Marbles. So anyone in either of those families will execute that code. Now sometimes it's great we've got that if but we don't want to have to write conditions for every single possible scenario. So in that case we can use the else tag. If we don't meet the condition above, then of else we're going to run this code. Now this isn't a tag on its own. You can't use it outside of an if tag. It's just a companion. It doesn't have an end else, it just goes right inside and that's your condition for, okay, we didn't meet the if, we'll do this instead. So here we're checking if current person dot last name equals Decker, we'll say hello Decker family and if it's not, we'll just print hello. We can also have multiple conditions that we check and this, the else if tag, is used very similarly to the else. It's a companion, doesn't have an end else if but we're just gonna check these in order. So we're gonna start with looking if current person dot last name equals Decker, we'll print hi Decker family. Next we'll check if the current person dot last name equals Marbles, we'll print good day Marbles family and if we didn't meet either of those, then we'll go down to our else and just print hello. It's important to note that these if blocks are gonna stop processing as soon as one of their conditions is met. So in this example here, we've got current person dot last name equals Decker for our first check, else if current person dot nickname equals Ted. Well, if Ted Decker is looking at this, as soon as we hit that current person dot last name equals Decker, we print that hi Decker family and then we leave the block. We don't also process the current person dot nickname equals Ted. Even though it's true, we already met our condition so we're done. For loops are how we repeat sections of code, sections of logic. It's going to create a new variable for us to use inside the for loop and that variable, every time the loop runs, will just contain the next item in the list. So in this example here, we've got our tag name for, then the next word here is current group membership and that's a name that I gave our custom variable that will be used. You can put whatever you want there. Then we use the keyword in to indicate, hey, this is the list we're looking in and here we're gonna look at current person.members which just contains a list of all the group memberships for the current person. So every time this loop runs for every single group membership that the current person has, current group membership is going to contain that group member item. So here we're just going to print the group's name and then do a line break to put on a new line. So this loop would print the name of every single group that the current person is a member of. And it's important to note that that custom variable that you set in the for tag only exists inside the loop. You can't access current group membership after the end for tag. It will just be empty. We can also add some additional parameters to change the way our for loop runs. So obviously if we were trying to print a list of every single group membership that someone was a part of, If you've been in the Rock instance for a while, that might be like 40 things. So we may want to limit that and say, well, only print the first three. We can also use offset to say, I don't care about the first five items in this list. Start at number six and then keep going. You can set that offset to just skip over the beginning of the list. You can also use reverse to say, I want to process this list last to first. So here we've got an example where same thing for current group membership in currentperson.members, but this time we're limiting it so it only prints the first three group memberships. Alright, let's take an example here. So the idea is we've got our public webpage and if the current person viewing already serves in our church then we just want to say, hey, thanks for serving. But if they haven't, then we want to present some kind of call to action like, hey, are you looking for a place to serve? Can we get you connected? So the first thing we're gonna do is create a variable called on serving team and we're gonna initialize it to false because we're gonna assume that the current person is not serving. Next, we have our for loop and we're gonna loop for through every group membership in currentperson.members. Right? So we've highlighted our variables are all under orange. Properties are that teal color. Next, we'll check if the current group and membership are active. Right? Because if you have a group membership but it's for something inactive, then you're not currently serving. So we're going to check if the group membership is active and the group itself is active. We're going to verify that the group's group type has a purpose and that that purpose is serving area, and that's how we can filter down to say, okay, just the serving team groups. We don't care about your family group or your small group. We're just looking at serving teams. And if that's true, well then we'll go ahead and switch our on serving team to true. Then down here, we actually have our if statement and we're using that variable. So if those things were not ever true, if we didn't ever hit a scenario where we had a serving group of type serving area that was active, then on serving team would still be false and that's where we would know, okay, we can say that looking for a place to volunteer. But if it's true, then we can say, hey, thanks for serving. Let's get into Lava filters. This is a keyword that allows us to mutate a variable's value. We can do things like numeric operations, addition, subtraction. We can format date values. We can modify date values or change the case of text. We can also do some simple operations like get the first or last item from a list, sort a list, group the list by a common property, get information like group membership, get the value of an attribute for an entity, or get the JSON value of a variable. JSON stands for JavaScript Object Notation and it's just a standard format that we use to print all the data in a variable on our server. So it would just list out with kind of a key value. This is the name of the property and here's the value of it. The syntax for filters is always gonna look the same. We're gonna have our input, the name of the filter, and then any parameters that we have. So here you can see we've got one. Current person is our input, the filter is attribute, and our parameter for that is allergy. So this would print out the value of the allergy attribute for the current person. So let's take that serving example and try to simplify it a little bit because we don't really want to have to loop through everything. So instead, we're gonna get a subset of that list of group memberships using the where filter. So here, we're gonna assign our active memberships to current person dot members, the list of our group memberships, then we're gonna say where our group member status is active. So that's only gonna save in that active memberships variable the group memberships that are active. Then we're going to use the select filter to transform that entity from group memberships to groups because now we want to check if the group is active. So we've still got our first line the same. We've got our active memberships. Now we're gonna use that variable, transform it to a list of groups, and then check where is active equals true, and now active groups just has a list of all the groups that are active for all our active memberships. Then we'll transform that list of groups to the group types and then finally the group type purpose all the way down so that we can check if the group type purpose is serving area. So at the very end of this, we get down to our serving teams is a list of group type purposes where value is serving area. And we know that if that contains anything, if it has a size, then they're in an active serving team. Well now we can actually chain these together one after the other instead of creating new variables and doing all of these on each new lines and we can just assign number of serving teams and here we've just got everything changed. Current person dot members where the group member status is active, switch that to a list of groups, filter to those that the group is active, select the group type, select the group type purpose, and then filter down to where the value is serving area. Then we're gonna use the size to just get how many items are in this list, and if it's more than zero, they're serving. Great. And if it's not, we can do our call to action. Hey, are you looking for a place to serve? You might be wondering though, okay, how do I know that current person has a dot members, that has a dot group, that has a dot group type, that has a dot group type purpose value? That's a lot to remember, right? Well, that's where the toJSON filter really comes in handy. You can also sometimes use the model map. That will help you out too, but I really like the toJSON filter because it just turns your variable into a very easy to read value. So if we did current person Members, then just take the first one because we don't want a ton of data, and then use that to JSON filter. By default, it's gonna print out something like this. Okay, not very readable yet, but we can toss it in a formatter for JSON. You can do that with an online one, but I would just be careful about that so you don't put anyone's personal information online or you can use a tool like Visual Studio Code who has formatters in there. Well once we format it, it kind of looks like this. That's a lot easier to read, isn't it? So we can see if you remember all those different properties. We've got an ID. For our group memberships, we've got a group ID, a person ID, a role ID, then we even have an object for the group role itself. So to read JSON, all of our there we go. All of our objects are going to be defined by curly braces. We'll have an opening curly brace to say this is the start of my object and a closing curly brace to say this is the end. All of our properties are gonna be written in key value format where key is gonna be in double quotes, there'll be a colon, and then the value. Values can be text, numeric, boolean, dates, objects, and arrays. Array is very similar to a list. It's a collection of items. We're going to define these with square brackets and arrays we can have an array of text, of numbers, of objects, of other arrays. It can really be anything. It's just a collection. It's important to remember when you're trying to use JSON a lot of people get confused and they try to access properties on arrays, but arrays don't have properties. The objects in them would, but sometimes when you're doing your dot notation, like how we did current person dot members to get the group membership, Some people think they might be able to, on that dot members, do a dot something else, like dot group ID, but that's a list, so it doesn't have that. You'd have to get the first item in it to do that. So let's look at the JSON for a person record. We've got our ID, our first name, our last name, and our connection status value, which is an object that tells us, hey, this person is an attendee. Then we also have phone numbers, which is an array of phone number objects that have their ID, the number, and the number type value, like this is a mobile phone or a work phone. But connection status value isn't actually a column in the person table. Our tables in the database don't store objects but they do store IDs that relate to something else. So our person table has a connection status value ID and this is gonna line up with a record in the defined value table that gives us the value and the defined type, in this case connection status. Well our server is smart enough to know that if we've got connection status value ID on our person record, that's not really useful information to us when we're trying to do lava. So it's going to take that and it's going to give you the whole corresponding record from the defined value table And that's what's going to make it really easy then when we're looking at our person record. Okay, we've got the connection status value ID, which is actually the property in our table, but we're going to make this fake property for you of connection status value, and now you can access that with dot notation. Current person dot connection status value dot value, and that prints the information we were looking for. This person is an attendee. Attribute values, like connection status and is a defined value, are not stored in the person table. They're different. So the attribute values table is going to give you an attribute ID that says this is the attribute and the entity ID it applies to, what record, and then the value. So in this case here, our attribute ID one, well, that's the allergy attribute, and two is the school attribute. And then the entity, well we've got some person records here. So this is how it pieces it all together so that then when you are doing your lava, you can have this information available and know this. But if we look at the attribute values array, it's kind of terrible to work with because it doesn't actually give us the full attribute object. It only gives us an attribute ID. Well, we don't want to have to go through and say, okay, person dot attribute values where the attribute ID is equal to 740, get the first value, then get the property of value to actually print that. That would be a nightmare. So instead they've built a Lava filter to get the attribute for any entity. So you can just use the attribute filter and then use the key of that attribute that you've set up to print the values that you don't have to do, try and wade through the attribute values array. So when I'm talking about the key, I'm talking about this value right here. Whenever you've set up an attribute in Rock, this should hopefully look familiar. You give it a name, description, field type, that key is how we're accessing this specific attribute on the entity. Some other filters that are really useful, the date filter. You can either give it a string that is an actual date or the keyword now to turn the current time into a date. You can add days on to that then format it with the date filter once again. You can check if a person belongs to a group with the group filter or you could even do things like list the nearest campus to a person. Definitely go to the Lava docs and check those out. Lava commands is gonna be the last section we go over. These are super powerful unlike oh, they're keywords that allow our server to process things like get data from the database, group memberships, financial transactions, registrations. We can even run custom SQL queries, create custom interactions, launch workflows, even more. The lava commands have to be enabled though. By default, they're not just gonna be there and ready and available. You have to go into your block settings and actually say, I want to be able to use this command. Because they're so powerful, because they access our database, we lock that down and we don't just make that available everywhere. So to retrieve data with the entity command, we're gonna take the name of the entity type, make it lowercase and one word. So to get group members from the database, we'd use group member, all one word, and when we're done with that entity, we're gonna use the keyword end in front of it, end group member, should look very similar to our tags. We also though have to provide information about what we're trying to get from the database. So for the entity command we can either pass an ID which is a specific ID of one record in the database. We can pass a comma separated list of IDs with the IDs parameter or we could just pass a condition like where group ID is equal to four and that would give us a list of everyone, all the group memberships where the group ID is four. Now to actually use that, it's gonna create a variable for you to use within that lava command. If you're getting just a single record, then it will be the same name as the entity command. So one, it will just be group member, then we can do, you know, dot person dot first name. Either of the other two, like IDs or where, it's gonna return group member items with a capital I, and that is a list of all of the data that met your criteria. So we could either, you know, grab the first one and print or you can see there I've got that group member item square bracket zero square bracket. That says the first one in the array. We can use, it's called indexing to access it and so we just put a value in there but remember that the first item is not going to be number one, it's going to be zero and then go up. Don't play around with the SQL commands. It literally allows you to run SQL directly on your database. That means you could drop a database table or delete your database. If you were not familiar with SQL, I just wouldn't mess with it. I would just skip over it, have someone else do that for you. Because it can be very dangerous. You can also open yourself up to SQL injection attacks. There's commands like personalize that if you've set up those personalization segments or filters, you can use the key of those to then have some dynamic content displaying like we were doing with if statements earlier. It's important to note for personalize where in if we said if and else, personalize will use otherwise but it's kind of the same idea. If somebody matches all these or any of these depending on what parameter we send it, we'll show this. Otherwise, we'll display this other default text. Some other notable commands, calendar events. Given a calendar ID and an audience, it will give you a list of these scheduled instances. Event scheduled instance, if you give an event item ID, you can get all the upcoming occurrences. You can actually write interactions to your database or launch workflows. We don't have enough time to go over all of them unfortunately, but definitely look at those Lava docs. So our resources. Obviously the community site, there are a ton of Lava documentation, and then there's also this Rock Shop plugin called the Lava Tester that Central Christian made. It is super helpful. It allows you to just test things out without needing to create a whole new page. Okay, I think we maybe have a little time for questions? Great.