Var-ception

October 4, 2019 Josiah Feuerbacher

This week’s post is going to be for some of the more advanced users. If you’re not familiar with variables or expressions, you might want to put this one on the back-burner until you read up on those topics.

We’re going to look at an obscure but highly powerful little expression function called var(). The var() function takes a string input and returns the value of the variable with that name. So in essence, these two things are the same:

This is obviously not overly helpful. The var() function becomes useful when you use markup to dynamically change the variable being referenced. Below are just a few examples:

Now for a real-world use case.

Let’s say you build a live data item displaying a list of US cities and states (with fields named {city} and {state}). When a user selects a row of the live data, you want to increment (or add 1 to) a variable. The name of the variable should correspond to the name of the state. So if a user selects Denver, Colorado, you want to add 1 to a variable named “Colorado”.

To accomplish this, you’re going to use the Player.SetVariable. As a reminder, it looks like this:

The variable name is pretty straightforward. You want to pass in the selected state from your data source:

However, the value of that variable is not as straightforward. You want to add 1 to the existing value. If you already knew the name of the variable, you could do this:

Since you don’t know the name, your first instinct might be to do some workaround, like checking the state against each possible value:

I’m going to stop you right there. While this does technically work, it’s a ton of work, not elegant, and could cause a pretty big slow-down as the Content Player processes the 50 expressions. Yuck.

Next, you might try to substitute the field name directly into the variable brackets (I call this “var-ception”):

But that syntax doesn’t work. You can’t place field names or variables inside of other variables in this way.

This is where the var() function comes in handy. With the var() function, you can reference your variable name dynamically. Check it out:

That’s it. Those 50 individual expressions compressed down to 21 characters. For the record, the entire script would look like this:

Pretty slick, huh? That’s it for today. Did you follow along?  Do you want more?  #DoYouFWI?

Previous
How Digital Signage in Schools is Transforming Education
How Digital Signage in Schools is Transforming Education

DIGITAL SIGNAGE IN SCHOOLS IS ONE OF THE MOST EFFECTIVE WAYS TO BRING EXCITEMENT AND ENGAGEMENT BACK INTO T...

Next Up
How to Create a Successful Customer Experience Using Digital Signage
How to Create a Successful Customer Experience Using Digital Signage

Here are six tips for creating digital signage displays that can improve your customer experience.