Added by Dennis Dam, last edited by Jasha Joachimsthal on Jul 17, 2009  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

Introduction

Nested structures are often used for hierarchical data, such as taxonomy or menu documents. In the CMS' editor, a nodetree structure is used to fill dropdowns or lists used in a picker. This is an example of a nodetree XML document:

<nodes>
  <node id="someKey1" value="value1"/>
  <node id="someKey2" value="value2">
    <node id="someKey2_1" value="value2_1"/>
  </node>
</nodes>

Fairly simple. Now let's move on to the matter of how to create a template for this nested structure. I will discuss the three different files to be created: XSD schema, layout.xml and business-logic.

XSD schema

To define a nested element in a XSD schema, you define the nested element, and add an inline reference back to the nested element. Inline definition is the only way, referring to a globally defined element will not work! The nested XML schema looks like this:

<xs:element name="nodes">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="node" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="node" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:attribute name="id" type="xs:string"/>          
          <xs:attribute name="value" type="xs:string"/>          
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Make sure you have minOccurs="0" maxOccurs="unbounded" (or some other numbers) on the sequence. Otherwise you can have problems like this: No element found for ft:widget[@id='xxxxxxxxxxxxxxnodesxxxxxxxxxxxxxxnodezzzzzzzzzzzzzzzcrepeatadd']

layout.xml

In the layout template you define the nested element as you always define a repeated element (see the explanation about the repeater widget), but inside the template for the repeated element, you need to use the reference element, to indicate that the repeated element should be nested inside itself. It looks like this:

<template name="/nodes">
  <!-- add button for the repeated element -->
  <action ref="/nodes/node" action="add"/>
  <!-- call the template for the repeated element -->
  <display id="/nodes/node"/>
</template>

<template name="/nodes/node">
  <div class="container">
    <!-- display the fields of the node element -->
    <textfield id="/nodes/node/@id"/>
    <textfield id="/nodes/node/@value"/>
    <!-- start nesting the current element in itself. -->
    <reference id="/nodes/node"/>
  </div>
</template>

The reference element is similar to the display element, except that it indicates nesting to the CMS' editor.

business logic

Nothing has to be done here!

Complete code

The nodetree backend template code can be downloaded here. It was tested in HippoCMS branch 6.05.03-dev. It should work for HippoCMS releases 6.05 or higher.