NAME

Krang::Element - element data objects


SYNOPSIS

  use Krang::ClassLoader 'Element';
  # create a new top-level element, passing in the class name and
  # containing object
  my $element = pkg('Element')->new(class => "article", object => $story);
  # add a sub-element
  my $para = $element->add_child(class => "paragraph");
  # add data to the sub-element
  $para->data("some test data here");
  # get a reference to the parent of $para, aka $element
  $parent = $para->parent();
  # get a reference to the root element of this tree, also $element
  $root = $para->root();
  # another way to add a paragraph, this time in one step
  $element->add_child(class => "paragraph",
                      data  => "some test data here",
                     );
  # save the element to the database, cascading through children
  $element->save();
  # make some changes
  $element->add_child(class => "horizontal_rule");
  # make a copy of the element tree
  $copy = $element->clone();
  # loop through child elements, printing out data elements
  foreach ($element->children()) {
      print $_->display_name, " => ", $_->data, "\n";
  }
  # same thing, but recurses through children of children too
  use Krang::ClassLoader Element => qw(foreach_element);
  foreach_element { 
      print $_->display_name, " => ", $_->data, "\n";
  } $element;
  # find a list of all paragraphs in the tree, using XPath-esque
  # notation
  @para = $element->match('//paragraph');
  # get the first paragraph of the second page
  ($para) = $element->match('/page[1]/paragraph[0]');
  # get a list of potential additional child classes, taking into
  # account max setting
  @classes = $element->available_child_classes();
  # load a top-level element by id
  $element1 = pkg('Element')->load(element_id => 1, object => $story);
  # delete it from the database
  $element1->delete();


DESCRIPTION

This module implements elements in Krang. Krang elements belong to a single element class, see Krang::ElementClass for details. Krang elements exist to contain child elements and/or store data. All complex functionality, like publish() and display_form(), is proxied to the element class.


INTERFACE

METHODS

PROXIED Krang::ElementClass METHODS

All Krang::ElementClass methods are proxied to the class object for convenience, with the exception of children() and child(). For example, you can write:

  $display = $element->display_name();

Instead of the equivalent, but longer:

  $display = $element->class->display_name();

For methods which take an element => $element parameter, this paramter will be automatically filled in when called through the proxied method. For example, you can write:

  $element->input_form(query => $query);

Which is equivalent to:

  $element->class->input_form(element => $element,
                              query   => $query);


TODO