ArcGIS Online

Manage your strings quite literally with Arcade 1.11

In the past, we have focused our blog articles on how you, as a map author, can leverage Arcade to present a clearer picture of your maps and data to your intended audience.  This article is not about that—in fact, it’s really all about you, the expression writer.  With the latest release of ArcGIS Online (and next release of ArcGIS Pro), we’ve introduced a new concept to make your code more readable and easier to follow.  That concept is string template literals.  Despite its long name, they simplify your life when you’re building complex strings.

If you are familiar at all with JavaScript, then these are exactly what you think they are. If not, let me explain.  Often when working with Arcade, you’re combining attributes and text together to create a meaningful sentence from your data. To achieve this, you typically take the approach of using Concatenate() or simply adding them together using plus signs:

return “This is quite simply the “+ $feature.Adjective + “ piece of text I’ve ever seen.”

Arcade would evaluate the text above and substitute $feature.Adjective in for the features value for that field.  In the end, if the value of Adjective happened to be best, the returned string would look like this:

This is quite simply the best piece of text I’ve ever seen.

If you’ve ever taken this approach, you’ve likely run the expression only to find that you forgot a leading or trailing space inside the quotation marks. If it’s a longer piece of text, all the + signs start to decrease the readability of the expression.

This is where string template literals come in.  They get rid of the + signs and help create a templated string where variables are substituted in. To create one, you enclose your string in backticks instead of quotes, and anywhere you’d like to reference a variable, you enclose it in ${}.  The example above would change to:

return `This is quite simply the ${$feature.Adjective} piece of text I’ve ever seen.`

By substituting in your variables and using template literals, you essentially create your own data-driven Mad Libs.

Also using string template literals make it easier to add line breaks into your text. You can simply hit enter to break the string into multiple lines.  Here’s a quick example that is a little more practical.  Let’s create a label expression that shows the status of the inspection, and who it is assigned to.  With each property on a new line.

Screenshot of multi-line labels
return `Status: ${DomainName($feature,"InspectionStatus")}
Inspector: ${DomainName($feature,"Inspector")}`

inside the template literal just use the enter key as if you were typing, and it will be honored when displaying the information (Multi-line labels only work in apps that use the 4.x  ArcGIS API for JavaScript such as the Map Viewer beta).

As always, if you’re looking to learn more or if you’re just getting started with Arcade, you can always get more information on the Developers website, check out a Learn lesson, or browse all the other great Arcade blog articles.  And be sure to check out the release notes tomorrow for more great Arcade features.

About the author

Product Engineer for ArcGIS Online and technology enthusiast.

Connect:
0 Comments
Inline Feedbacks
View all comments

Next Article

Tighten Up Your Edits with Editing Constraints in ArcGIS Online

Read this article