Posted on by

Using the PDb_Template class in your custom templates

Participants Database 1.5 has a new utility class that was created to make templating much easier. The default templates used by the plugin are based on loops, much like the standard WordPress templates. Loops are the best way to deal with a situation where you don’t know the number of fields to be shown: the template just keeps showing fields in a repeating pattern until they have all been displayed. Loops are also an efficient way to represent many similar pieces of content.

That may be convenient, but displaying data in a meaningful way often demands you treat parts of the dataset differently. Some of those fields are just more important than others, or perhaps some pieces need to be arranged on the page in a way that best suits the content.

The PDb_Template Class

The PDb_Template class adds several convenient methods for displaying the data in a record. The class is instantiated once for each record, so if the page is a single record, this happens once at the top of the template. For a list of records, the class is instantiated in the loop after the record has been read from the database. I’ll show you an example of a single record template that uses the PDb_Template class for it’s layout.

This template takes several of the more important pieces of information from the record and presents it in a much more readable format. Other fields are logically laid out in groups below that. Below all that, the remaining fields are shown using a shortcode that prints a loop of the groups those fields are in.

This template starts with the instantiation of the Template class near the top, before anything is output. With that, the variable $this_business has all the data of the current record, as well as several convenient functions to help you set up your template.

The main method you will use is print_field('field_name'). Just give it the name of the field you want to print and the method will echo the value formatted for display. It’s mostly self-explanatory.

Raw values are available for calculations or custom formatting using the get_value('field_name') method, demonstrated where we show the number of years the business has been around. Dates are stored as UNIX timestamps, so the raw values from that field needs to be converted to a year for the calculation.

A method named has_content('field_name') can be used to skip over fields that are empty. You see that in use where we want to show the photograph if it exists.

At the end of the template, we use the WP function do_shortcode() to print out the rest of the fields, which are contained in the “More Details” field group. This shortcode uses a special template designed for this purpose: it just does not print the wrapper div because it will appear inside one.

There are a few more convenient methods you can use, which I will demonstrate in the next tutorial on using the PDb_Template utility class.

74 thoughts on “Using the PDb_Template class in your custom templates

  1. Hi Roland,



    This is a question is in regard to customising a record template so it is editable, as posed by Dennis Spiess on 17th March 2020 above.



    I have followed all his actions but I am having problems in finding where to place his code line
    ( $record->print_form_element(‘is_100_date’);?> ) in the custom template.

 (pdb-record-default)

    Would you have an idea on the best line to insert that code line? Or maybe Dennis may see this post and also offer assistance.

Thank you

    1. Hi Philip,

      I’m sorry, I can’t really help you with this, not only do I not know how to answer your question (too general), but I don’t provide support for custom code.

      1. Thank you mate,

        Maybe Dennis might see this post and offer his line of action.
        Cheers

      2. Hi again Roland,
        Thanks for your ongoing support and hopefully you could help with this issue.

        I have custom templates for the [pdb_record template=' '] shortcode. It seems when using the custom template and I save a record after editing it, , it deletes the all the data in that record and returns to the default values.

        Within the template I am also using…

        $record->get_value(‘fieldname’)
        $record->print_value(‘fieldname’)

        so I’m not sure if these code lines are affecting the saving function or if there is something else that you may have experienced to this effect that may help resolve this issue.

        Any ideas on what is the cause and possible solutions?

        1. Assuming you are certain there are no problems in your custom template (that part is up to you), you’d turn plugin debugging on, then test your record save. Check the log, you’ll see what happened.

          I don’t know of any issue that would cause this specific effect. Make sure the template is not missing any of the data, it sounds like maybe what is happening is it’s creating a new record, which it would do if the record ID was missing, for example. It is not at all normal to replace a records existing data with default data.

        2. There is no problem with the save function when using…
          [pdb_record fields='field1,field2,field3'] shortcode.

          I made a quick 2 minute screen recording to help with explaining.
          https://youtu.be/w0SXZDPaxAE

        3. As far as I can see from that, there is a problem with your custom template. You can use the debugging log to see what’s happening, but you need to make sure your custom template does not contain coding errors.

Leave a Reply

Your email address will not be published. Required fields are marked *

74 thoughts on “Using the PDb_Template class in your custom templates

  1. Hi Roland,



    This is a question is in regard to customising a record template so it is editable, as posed by Dennis Spiess on 17th March 2020 above.



    I have followed all his actions but I am having problems in finding where to place his code line
    ( $record->print_form_element(‘is_100_date’);?> ) in the custom template.

 (pdb-record-default)

    Would you have an idea on the best line to insert that code line? Or maybe Dennis may see this post and also offer assistance.

Thank you

    1. Hi Philip,

      I’m sorry, I can’t really help you with this, not only do I not know how to answer your question (too general), but I don’t provide support for custom code.

      1. Thank you mate,

        Maybe Dennis might see this post and offer his line of action.
        Cheers

      2. Hi again Roland,
        Thanks for your ongoing support and hopefully you could help with this issue.

        I have custom templates for the [pdb_record template=' '] shortcode. It seems when using the custom template and I save a record after editing it, , it deletes the all the data in that record and returns to the default values.

        Within the template I am also using…

        $record->get_value(‘fieldname’)
        $record->print_value(‘fieldname’)

        so I’m not sure if these code lines are affecting the saving function or if there is something else that you may have experienced to this effect that may help resolve this issue.

        Any ideas on what is the cause and possible solutions?

        1. Assuming you are certain there are no problems in your custom template (that part is up to you), you’d turn plugin debugging on, then test your record save. Check the log, you’ll see what happened.

          I don’t know of any issue that would cause this specific effect. Make sure the template is not missing any of the data, it sounds like maybe what is happening is it’s creating a new record, which it would do if the record ID was missing, for example. It is not at all normal to replace a records existing data with default data.

        2. There is no problem with the save function when using…
          [pdb_record fields='field1,field2,field3'] shortcode.

          I made a quick 2 minute screen recording to help with explaining.
          https://youtu.be/w0SXZDPaxAE

        3. As far as I can see from that, there is a problem with your custom template. You can use the debugging log to see what’s happening, but you need to make sure your custom template does not contain coding errors.

Leave a Reply

Your email address will not be published. Required fields are marked *