The PDb_Template helper class makes setting up a custom display for a Participants Database record much easier in situations where you don’t want to just loop through the values. Sometimes you don’t want to treat each value the same, you want to lay out each value in a way that fits the information better. This is demonstrated for a single record in the post Using the PDb_Template class in your custom templates.
You can also do something like that in the list template by instantiating the PDb_Template class for each record in the list.
First, make sure all the fields you want to display are configured to be included. You can do this either on the Manage Database Fields page (using the “display” column) or in the list shortcode with the “fields” or “groups” attributes. This is explained here…
How the List Template Works
The list template works by looping through each record in the list, whether it’s showing all the records or a search result. Normally, for each record, there is another loop that shows the field values of that record. This is very flexible and convenient, but it may not give you the look you want in your results because each value is treated the same visually.
In this post, I’ll demonstrate a simple application of the use of the PDb_Template class to lay out the record’s values in a different way. Let’s say you’re using Participants Database as a directory, a list of people who offer a service, for instance. For this, each record is going to be displayed as a set of contact information something like this:
Chiropractic, Physical Therapy
Doing something like that is simply a matter of figuring out the HTML you want to use to display the record, then use the template class to fill in the values. In our template, we will use HTML like this to show each record:
<h3>Bernard Carmen</h3> <h5>Chiropractic, Physical Therapy</h5> <p>Scottsdale, Arizona</p> <p><em>Email: firstname.lastname@example.org</em><br /> <em>Phone: 002-332-2212</em></p>
In the template, using the helper class, that will look like this:
<?php $record = new PDb_Template( $this ); // instantiate the helper class ?> <h3><?php $record->print_value( 'first_name' ) ?> <?php $record->print_value( 'last_name' ) ?></h3> <h5><?php $record->print_value( 'services_offered' ) ?></h5> <p><?php $record->print_value( 'city' ) ?>, <?php $record->print_value( 'state' ) ?></p> <p><em>Email: <?php $record->print_value( 'email' ) ?><br /> Phone: <?php $record->print_value( 'phone' ) ?></em></p>
The Custom List Template
Below, we show the complete list template so you can see how that fits in. We’re using the “responsive” list template for our stating point, since it’s already set up to show each record as a block of data instead of a row in a table. The individual record display code happens after line 80.