Client-side templates

The client-side templating provides a mechanism that allows the Client Framework 2 page authors to separate the presentation layer from the data. This mechanism is intended to be used when the client-side needs to display the data that can change over the lifetime of the page on the client. Templates are just XDIME pages containing slots and/or conditional statements. Slots are placeholders that are dynamically filled with the data, and conditional statements can be used to select proper elements for processing. The data must be provided as JSON either in the initial page, or as a response to an AJAX request.

The client-side templating provides a mechanism that allows the Client Framework 2 page authors to separate the presentation layer from the data. This mechanism is intended to be used when the client-side needs to display the data that can change over the lifetime of the page on the client. Templates are just XDIME pages containing slots and/or conditional statements. Slots are placeholders that are dynamically filled with the data, and conditional statements can be used to select proper elements for processing. The data must be provided as JSON either in the initial page, or as a response to an AJAX request.

The client-side templates can:

Current components and default properties

Behavior of various Client Framework 2 features depends on the context within which they are used. The context consists of the following two aspects:

Current component

This is the id attribute of a component. If no id was explicitly supplied by the page author, then this is the automatically generated identifier.

Default property

This is the identifier of the property, for example, the value that would be stored in the property attribute of the cf2:param element within the cf2:on element. If this does not explicitly specify the component, then it is relative to the current component, which has just been set.

The context can be changed during processing of XDIME elements, and applies to the element that changed it and its body. Changing the context changes all aspects of the context, e.g. it is not possible to change the default property without also changing the current component.

Slots

A slot is a placeholder that is populated with the content when the data bound to it is set or changed. Each slot is bound to an observable property and has a number of associated JSON paths. The slot responds to changes in the property's value by evaluating its paths against the value and using the results to update the page accordingly.

Slots and conditional statements support optional component and property-value attributes. The component attribute references a component. The client-side value is the object representation of the component. The property-value attribute specifies the property against which the slot will evaluate its paths.

The Client Framework 2 supports three types of slots:

Name Purpose
cst:a

Defines an anchor slot within a client-side template.

cst:image

Defines an image slot within a client-side template.

cst:value

A template slot which allows the body content of an element to be set from the data in the template.

Conditional statements

Conditional statements determine whether the content is displayed on the client or not. This functionality is provided by the cst:switch element. It can contain one or more cst:case elements followed by an optional cst:otherwise element. The cst:case element tests the value of the cst:switch element against a fixed value, and if they are equal, then the element is selected for processing. The cst:otherwise element specifies a case to be selected for processing if none of the cst:case elements within the containing cst:switch element are selected. Please note that conditional statements do not affect any nested components or scripts. In particular, the script elements behave in the same way as if they were outside the conditional statements.

Name Purpose
cst:case

One of a number of possible matches which could be selected when processing the cst:switch element.

cst:otherwise

Specifies a case to be selected for processing if none of the cst:case elements within the containing cst:switch element are selected.

cst:switch

Container for one or more cst:case elements and an optional cst:otherwise element. It is used to choose between a number of mutually exclusive alternatives.

JSON paths

Selects a specific object from a JSON data structure in a similar way as XPath allows for traversing over an XML document. The following table lists JSON expressions supported in the Client Framework 2.

Path
Value: Self | Steps
Self
Value: '.'
Steps
Value: FirstStep | FirstStep NextSteps
FirstStep
Value: Property | Array
NextSteps
Value: NextStep | NextStep NextSteps
NextStep
Value: '.' Property | Array
Property
Value: SimpleProperty | StringProperty
SimpleProperty
Value: [a-zA-Z][_a-zA-Z0-9]*
StringProperty
Value: '"' [^"] '"' | "'" [^'] "'"
Array
Value: '[' [0-9]+ ']'

The paths are processed as a series of sequential steps. The processing of a JSON path starts with an initial object that is context dependent. If the path is simply '.', then the initial object is returned as the result. Otherwise the initial object is used as an input for the first step, the output from the last step is the result of processing the path, and the inputs to and outputs from the intermediate steps are exchanged between them. If, at any point, a step returns null, then the processing stops immediately and the path is assumed to have a null value.

There are two basic types of steps:

The following table provides several examples illustrating the use of JSON paths.

JSON path Description
<cst:a href="."/> Selects the object against which the path is to be resolved.
<cst:a href="xyz"/> Selects the 'xyz' property of the object against which the path is to be resolved.
<cst:a href="[2]"/> Selects the 2nd item of the array against which the path is to be resolved.
<cst:a href="xyz[2]"/> Selects the 2nd item of the 'xyz' array property of the object against which the path is to be resolved.
<cst:a href="'1 2 3'[2]"/> Selects the 2nd item of the '1 2 3' array property of the object against which the path is to be resolved.
<cst:a href='"1 2 3"[2]'/> Selects the 2nd item of the "1 2 3" array property of the object against which the path is to be resolved.

Related topics