OpenSocial has a set of reserved special variables for working with
template data objects and processing the template. These
variables—Context
, Cur
, My
,
and Top
—each have different groups of
data that they can access and a different order of precedence during
loading. With the exception of ${Context}
, special
variables are not required when you’re referencing data. Since the
variables are not required, expressions will be evaluated against the
available special variables using the following
precedence load order:
${Cur}
${My}
${Top}
The ${Context}
variable contains additional information—relating to
currently processed data objects and identifiers for the template—that
is used during the processing of the current template. This
information provides context for the data that is being processed or
rendered.
${Context}
contains three
variables:
Count
The total number of results available in the current data object being processed within a repeater structure. This alters the flow of a repeater element based on the total number of results.
Use: ${Context.Count}
Index
The current numeric index being processed within a
repeater structure. Much like the Count
variable, Index
allows you to alter the flow of
the loop based on the current object index processed.
Use: ${Context.Index}
UniqueId
Provides a unique identifier for the current template. This value can be used to generate custom IDs for DOM nodes in the template markup.
Use: ${Context.UniqueId}
The ${Cur}
variable
refers to the current data object being processed in the
OpenSocial templating process. One of its main uses is to refer to the
current item being iterated over during a loop through a data
structure with repeating sources.
<div repeat="${userData.profile}"> <img src="${Cur.image.thumbnailUrl}" alt="Profile Photo" /> Name: ${Cur.name}<br /> </div>
When you’re working with multiple data objects or large amounts
of data, ${Cur}
is a handy variable
to use to maintain context with the currently processed object. You
can also use this approach to promote code reuse if you’ll be
processing multiple objects with a similar structure.
Should you wish to set the scope of the data to which ${Cur}
maps, you can set that
value explicitly using the cur
attribute.
For instance, if we have the application owner’s friends
stored in a data object, ownerFriends
, we can set ${Cur}
to reference a particular friend,
and then we can use that to iterate through the URLs associated with
the user.
<div cur="${ownerFriends[2]}"> User Website Associations for ${Cur.name}: <ul> <li repeat="${Cur.urls}"> <b>${Cur.linkText}:</b> ${Cur.address} </li> </ul> </div>
This code will display the URLs for the owner’s third friend.
${My}
allows developers to access data objects that are passed in to
the template by using a custom tag on the OpenSocial template script
block.
<script type="text/os-template"
xmlns:app="http://www.mysite.com"
tag="app:userProfile">
The ${My}
variable will be
available only within script
blocks
that are invoked using these custom tags. When you use a data object
via ${My}
, the template will first
check for the object reference within the data that is passed through
via the tag. If it can’t find the object, the template will then look
for an element with the same key name.
<script type="text/os-template" xmlns:app="http://www.mysite.com" tag="app:userQuestionnaire"> User: ${My.user.name}<br /> Question 1: ${My.questions.q1.title}<br /> Answer: ${My.questions.q1.response} </script>
This variable provides an additional way to access groups of data structures within a template. If any of these data references were not available within the custom tag, the template would search for the data in the main objects.
${Top}
is a variable that provides references to all of the data
available within the current template, accessible by the data source
key. This reference variable makes it easy to mash up data sources,
compare objects, and perform a number of other functions.
<div If="${Top.Viewer.name == 'John Smith'}"> Hello ${Top.Viewer.name}! This is a personal message for you. </div>
52.15.80.101