Additional Template Attributes

Referencing Templates

All the templates used so far have had the template placed inside the root element with the datasources attribute. However, you may use the template attribute to refer to a template located elsewhere within the document. This allows you to share the same template among two different parts of the user interface. To use this technique, place a template attribute on the root element set to the id of a template.

<listbox datasources="template-guide-photos5.rdf"
            ref="http://www.xulplanet.com/rdf/myphotos"
            template="photoTemplate"/>
...
<template id="photoTemplate">
  ...
</template>

This template will be shared with any other element that references the id 'photoTemplate'. The listbox here does not have any children, although it may do. If you did add children, they act just like the static content as if the template was present. However, it is possible to use different static content for each usage, even though the template is shared. The datasources and ref attributes also differ for each usage, so it is possible to use a shared template to display the same structure multiple times but with different starting nodes in each case. The generated content is always inserted into the root node, in this example the listbox, not inside the template.

Declaring the Container and Member Variables

Normally, the container and member variables are determined by the template builder automatically. The container or starting node variable is specified in the <content> tag inside a query, while the member variable is determined by the value of the uri attribute inside the action body. It is also possible to be explicit about the two variables by using two attributes on the <template> element.

<template container="?first" member="?item">

The container and member attributes can be used to specify the container and member variables. This isn't particularly useful although there is a very slight optimization since the builder does not need to scan the action body looking for the member variable when compiling the queries and rules. You might also use these attributes just to make the code clearer when using very complex queries.

One possible advantage is when using the simple RDF query syntax where you don't specify variables; instead you use the special 'rdf:*' syntax for the member variable and the container is implied. By using the container and member attributes, you can use specific variables instead.

<hbox datasources="template-guide-photos5.rdf"
      ref="http://www.xulplanet.com/rdf/myphotos">
  <template container="?start" member="?photo">
    <rule>
      <image uri="?photo" src="?photo"/>
    </rule>
  </template>
</hbox>

In this example, the ?photo variable can be used instead of 'rdf:*' (although you can use either even if you specify the member variable). This may make the code clearer as to its function. We could use the container variable ?start in the action body also. If you are using the simple syntax and want to use the container variable in the content, you must use the container attribute since there is no other way to refer to it. Note also that a rule element is needed here, otherwise the builder will think the container and member attributes are conditions to check.