Conditionals
Using conditional statements, a developer can create portions of the template markup that will be displayed only if certain criteria are met. These statements are valuable for creating content targeted to a specific user base and can be used to build out a scalable, engaging template system within the application.
Conditional statements are evaluated as Boolean values, meaning
that null or empty strings will be
evaluated as false and content within
the node will not be rendered. In addition, conditionals may be applied
to any variable or node within an OpenSocial template, with the
exception of the os:If and os:Repeat variables.
There are two methods for defining a conditional statement within an OpenSocial template. In this example, let’s assume that we are starting with a data pipe that contains some user-based rating information about a series of movies.
movies: [
{
title: "The Social Network",
release: "October 1, 2010",
rating: 5
},
{
title: "Dinner For Schmucks",
release: "July 30, 2010",
rating: 2.5
},
{
title: "Alone in the Dark",
release: "January 28, 2010",
rating: 1
}
];Using this as our base, next we’ll explore how to create conditional statements to generate markup for the movies, with optional content sections for good or bad movies.
Method 1: Escaped values
The first method is to build a conditional structure where the data values within the block are output as escaped strings. This means that if the data value is:
<b><i>Text</i></b>
then the output of the data ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access