Looping Content

When you’re working with large quantities of user social data, one question naturally arises in the context of OpenSocial templating: how do you handle repeating content, such as a list of friends or any measure of activity data? We certainly won’t be creating a markup definition for each repeating node in the template, will we? The short answer is no.

Content looping in OpenSocial allows a developer to define a block of markup to be used to render a series of repeating elements, without having to resort to handling each repeating node as a separate instance of markup. There are a few ways to define a loop structure within an OpenSocial template specification, which we’ll go over next.

Method 1: Escaped values

The first method is to build a loop structure that outputs the value of a piece of data as an escaped string, much like we saw with conditional statements. Let’s say that we have a customized subset of activity data that is returned to us through a data pipe request:

activities: [
      title: "Jonathan just added a new comment on Twitter",
      body: "See what Jonathan is saying by adding him to Twitter",
      url: "http://www.twitter.com/jcleblanc"
      title: "Michael updated his profile",
      body: "See more about what Michael is doing",
      url: "http://www.container.com/profiles/michael"
      title: "Diane just posted a comment on your wall",
      body: "Reply to Diane's message",
      url: "http://www.container.com/messages"

This content structure has several repeating nodes within ...

Get Programming Social Applications now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.