Posted on by

Using the PDb_Template Helper Class in a Custom List Template

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:

Bernard Carmen

Chiropractic, Physical Therapy

Scottsdale, Arizona

Email: drcarmen@therapycenter.co
Phone: 002-332-2212

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: drcarmen@therapycenter.co</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.

7 thoughts on “Using the PDb_Template Helper Class in a Custom List Template

  1. Hello, this is very helpful. I was wondering if you had a list of the ways values could be printed? For instance, my “photos” and “website” column aren’t printing correctly when I use print_value. Are there different methods for those?

    Thank you!

    1. To be a bit more clear (I felt my question was lacking information)

      https://xnau.com/work/wordpress-plugins/participants-database/participants-database-documentation/participants-database-api/the-template-helper-class/

      Using any of the printing methods seems to just give a blank result, when the data is in the database. Other plain-text inputs are showing up properly

      1. Make sure that all the fields you want to display are configured to appear in the list. I’m going to add a note to the article that explains this.

        1. Ah, that makes sense. Got it – Thank you!

  2. Roland,
    This example is a great help. What if I still have more information shown on a single record basis (that I don’t want to show in the full listing)? How do you invoke the URL so that it makes the column you have chosen to be your link to the single record listing an actual link to take the browser to the page?
    Very appreciative of this plugin and your educational posts!
    Brian

    1. The helper class has a method for that. For instance if from our example, you wanted to link the name to the detail page, you’d do it like this:

      &lt;h3&gt;&lt;a href=&quot;&lt;?php echo $record-&gt;get_detail_link() ?&gt;&quot;&gt;&lt;?php $record-&gt;print_value( 'first_name' ) ?&gt; &lt;?php $record-&gt;print_value( 'last_name' ) ?&gt;&lt;a/&gt;&lt;/h3&gt;

      1. Aha!
        Thanks Roland!

        Brian

Leave a Reply
You have to agree to the comment policy.

7 thoughts on “Using the PDb_Template Helper Class in a Custom List Template

  1. Hello, this is very helpful. I was wondering if you had a list of the ways values could be printed? For instance, my “photos” and “website” column aren’t printing correctly when I use print_value. Are there different methods for those?

    Thank you!

    1. To be a bit more clear (I felt my question was lacking information)

      https://xnau.com/work/wordpress-plugins/participants-database/participants-database-documentation/participants-database-api/the-template-helper-class/

      Using any of the printing methods seems to just give a blank result, when the data is in the database. Other plain-text inputs are showing up properly

      1. Make sure that all the fields you want to display are configured to appear in the list. I’m going to add a note to the article that explains this.

        1. Ah, that makes sense. Got it – Thank you!

  2. Roland,
    This example is a great help. What if I still have more information shown on a single record basis (that I don’t want to show in the full listing)? How do you invoke the URL so that it makes the column you have chosen to be your link to the single record listing an actual link to take the browser to the page?
    Very appreciative of this plugin and your educational posts!
    Brian

    1. The helper class has a method for that. For instance if from our example, you wanted to link the name to the detail page, you’d do it like this:

      &lt;h3&gt;&lt;a href=&quot;&lt;?php echo $record-&gt;get_detail_link() ?&gt;&quot;&gt;&lt;?php $record-&gt;print_value( 'first_name' ) ?&gt; &lt;?php $record-&gt;print_value( 'last_name' ) ?&gt;&lt;a/&gt;&lt;/h3&gt;

      1. Aha!
        Thanks Roland!

        Brian

Leave a Reply
You have to agree to the comment policy.