Working With Nodes

Getting a Node

To get an object representing a node, call the WebTemplate::getNode function. The argument to getNode is either the guid of the node or the path to the node.

Example:

// get the contacts node by passing in the path of the node
$contactsNode = $GLOBALS["WT"]->getNode("/Contacts");

// get a contact node by using its guid
$contactGuid = 54356;
$contactNode = $GLOBALS["WT"]->getNode($contactGuid);

Creating a Node: To create a new node, call the createChild function of the parent node. The first argument is the type of node to create, the second argument is the attributes of the node to create.  The createChild function will return the object for the newly created node.

An Example: Creating a Contact Node.

// first get the parent node
$contactsNode = $GLOBALS["WT"]->getNode("/Contacts");

// make sure the parent node exists
if($contactsNode) {
  $attributes = Array("First Name" => "John", "Last Name" => "Smith", "Email Address" => "john.smith@domain.com");

  // call the createChild function
  $newContactNode = $contactsNode->createChild("Contact", $attributes);
}

Settings a Node's Attributes A node's attributes can be set using the WTNode::setAttributes function. The argument to setAttributes is an associative array where the keys are the names of the attributes and the values are the values of the corresponding attribute. Example:

// get a contact node by its guid
$contactNode = $GLOBALS["WT"]->getNode($contactGuid);

// if the node exists, set the first name attribute
if($contactNode) {
  $contactNode->setAttributes(Array("First Name" => "Jane"));
}