Jump to content

Am I using repeater fields correctly?


spos
 Share

Recommended Posts

I'm trying to build a fairly simple site as an intro to processwire. Currently on my to-do list is to have an "our customers" page with a list of testimonials, and also to have a random testimonial snippet picked from this list in a sidebar on various other pages.

So, I created a repeater field "testimonials" comprising testimonial_body, testimonial_summary and testimonial_name (the testimonial_summary is just a few choice words for the sidebar). Created a template "testimonial_list.php" and assigned the testimonials repeater field to it then made a page "our-customers" and gave it the template "testimonial_list".

So, I've got an /our-customers/ page that lists the testimonials:

foreach($page->testimonials as $testimonial) {
echo "<blockquote><p>{$testimonial->testimonial_body}</p>";
echo "<small>{$testimonial->testimonial_customer}</small></blockquote> ";
echo "<hr>";
}

All good so far, but now I'd like to add pagination. However, after reading the selectors and pagination API docs I'm afraid I have no idea how to proceed.

Should I even be using repeater fields for this? They weren't enabled by default so I'm thinking perhaps I should be using a separate page for each testimonial. I'm having a hard time figuring out how pages, templates, fields relate to each other.

Link to comment
Share on other sites

Should I even be using repeater fields for this? They weren't enabled by default so I'm thinking perhaps I should be using a separate page for each testimonial. I'm having a hard time figuring out how pages, templates, fields relate to each other.

You are thinking well here. Repeaters were created for a small number of repetitions. For a less predictable amount that justifies paginating would be better to use normal pages. In your case, you would have a page "testimonials" as a child of "our customers" and each children of "testimonials" would be one testimonial. The template for those pages would have the fields that you put on the repeater.

foreach($pages->find('template=testimonial') as $testimonial) {
echo "<blockquote><p>{$testimonial->testimonial_body}</p>";
echo "<small>{$testimonial->testimonial_customer}</small></blockquote> ";
echo "<hr>";
}

edit: It's also good to add that you are not forced to create a template file for all templates. In the case of the testimonials pages, if you are not planning to display them individualy, just create their template without a corresponding file, and their URLs won't be accessible in the browser.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...