The template helper class gives template developers an easy way to create templates with any desired structure.
The default templates work on the principle of a loop. This was chosen because it is familiar to people who work with WordPress templates and it is a very efficient way to lay out a lot of data. The shortcoming is it lacks flexibility: it’s really only good at displaying a series of data fields, it does not support display structures where different data types or fields need to be treated very differently, or if you want to show the data in an arbitrary order.
It is possible to combine the display of looped and non-looped values into a template, although this pretty much requires that the field groups support this organization by containing looped and non-looped fields in separate groups.
Instantiating the Template class
The Template class is instantiated once for each record and supplies all the data fields of the record, along with several helpful methods for presenting the data fields. The class is designed to be instantiated with the module class that generates the template. In the context of the shortcode template, this module object is referred to as
$this. So, we instantiate the Template class in the shortcode template like so:
$record = new PDb_Template($this);
This gives the handle
$record all the properties and methods of the template class using the data found in that particular record. This can be used in pdb_single, pdb_record, and pdb_list templates. Except for the pdb_list template, this would normally be done at the top of the template, making the properties and methods available to the whole template.In the pdb_list template, the class is instantiated after the record is assigned with the
$this->the_record() method, so it ends up getting reinstantiated for each record in the list.
The public properties
- record – this is the iterator that is looped through by the module to build it’s display. It contains only the fields that are to be displayed and is organized into groups. For the list module, which does not use groups to organize it’s fields, this property is empty.
- fields – this is a collection of field objects indexed by the field name, in the order set by the setup or the shortcode. It provides a convenient way to access the values of any field at random, or it can be iterated to use it in a loop.
- values – this is an array of raw field values and includes all the values of the record, including hidden, internal, and administrative values.
- groups – an array of group objects with name, title, description, and field name array properties.
- module – a string identifying the currently active module: signup, single, record, list, etc.
- base_type – this is simply the class name of the instantiating class…not really needed in a template context, but could be useful for generating CSS classes or something.
The public methods
- get_field_prop($name, $property)
- print_field_prop($name, $property)
- print_with_link($name, $URL)
Tests the named group for the presence of non-empty fields. Returns true if at least one field has a value.
Returns boolean true if the named field value is non-empty.
Returns the named value from the record. This is the raw, unformatted value.
Returns a property value from the named field object. This provides access to any of the defined properties of the field such as title, values, validation, etc.
Returns the URL of the record detail page. If a page identifier is provided, overrides the normal single record detail page with the provided page permalink.
Works like get_detail_link, but for the record edit page.
Allows the default edit or detail page to be set. This change only affects the specific instance of the Template class.
Echoes the formatted value of the field.
Echoes the title of the field.
Echoes the named property of the named field.
Prints the title of the named group.
Echoes the description of the named group.
Returns true if the group has a defined description.
Echoes the field value wrapped in an anchor tag with the provided href value.