Forum

Thread tagged as: Question, Problem, Configuration

List and Detail then Detail page

I have a basic list and detail page working OK and have the detail page so to act as group/category area to list using a Perch repeater...

<h1><perch:content id="title" type="text" label="Product title" required="true" title="true" /></h1>
<perch:content id="image" type="image" label="Image" width="640" output="tag" />
<perch:content id="desc" type="textarea" label="Description" textile="true" editor="markitup" />
<perch:content id="slug" for="title" type="slug" suppress="true" />
<perch:content id="image" type="image" label="Image" width="100" height="100" crop="true" suppress="true" />

Then I've added a repeater to loop and add individual items in the group/list...

<perch:repeater id="fabric" label="Fabric">
<h2><perch:content id="title" type="text" label="Product title" required="true" title="true" /></h2>
<perch:content id="desc" type="textarea" label="Description" textile="true" size="xs" help="Give some clear description of this product" />
    <img src="<perch:content type="image" id="image" label="Image" />" 
      alt="<perch:content type="text" id="alt" label="Image Description" />" />
<p>Price: £<perch:content id="Price" type="text" label="Price" format="#:2" help="Adjustment price" /></p>
</perch:repeater>

So far so good.

I'd like to ideally select one these repeated items to display it on it's own page, so it's kind of a List > Detail > Detail page.

Is this doable in Perch to single out individual repeated items? And how?

Or should I be approaching this in a different way?

David Owen

David Owen 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to do it in the template with a <perch:if>

How would <perch:if> single out one of the list of <perch:repeater> items?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need something unique from the repeater - how are you forming the URL to this detail view?

I've got this in the repeater (although not necessarily 100% unique once an editor gets hold of it)

<p><a href="shop.php?p=<perch:content id="title" type="text" />">Buy Now</a></p>

or perhaps could I grab 'id=perch_item_index', Would that be unique?

The plan was to just display that repeater item on shop.php

Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_item_index would be unique, but would change if the repeater was reordered.

It's more important how to filter one of the <perch:repeater> items to appear on shop.php

<p><a href="shop.php?p=<perch:content id="myrepeatertitle" type="text" />">Buy Now</a></p>
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you want to show item 2, you'd use

<perch:if id="perch_item_index" match="eq" value="2">
   ...
</perch:if>

Mmm.. not sure if I'm approaching this right.

I'd never know how many repeaters the editor would have added short of adding an exhaustive <perch:if> template for every value

Is <perch:if> the only way to do this?

<title>
<main-description>
<perch:repeater>
  <perch:if   ????>
    <product-title><link to sort/list only this item>
  </perch:if>
</repeater>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You don't need the if when generating the link.

<a href="whatever?index=<perch:content id="perch_item_index" />">

Sorry my bad yes. I'd still need it in the list template....

<title>
<main-description>
<perch:repeater>
  <perch:if   ????>
    <product-title>
  </perch:if>
</repeater>

The detail page is already sorted on...

../products.php?s=list-detail

And so link needs to be (to get the repeater for that item)...

../shop.php?s=list-detail&p=perch_item_index

Which I don't know how to get the value of 'perch_item_index' back into the template. (it all works if I add a match manually)

...meaning I need the first id="title" available to the <perch:repeater> inner part of the template

Doing a <perch:showall /> tell me that any ID "outside" of the <perch:repeater> is not available the inside of <perch:repeater>

Can this be done?

... scope-parent="true" might help me here. Let me test this.

OK scope-parent="true" let's me properly format the query string to make it something like this...

../shop.php?s=list-detail&p=single-repeater-item

And I have this on the shop page...

if (perch_get('s')) {

          // Detail mode
          perch_content_custom('Products', array(
              'page' => '/products.php',
               'template' => 'shop.html',
               'filter'   => 'slug',
               'match'    => 'eq',
               'value'    => perch_get('s'),
               'count'    => 1,
          )); 

     } else {

          echo "<p>nothing here</p>";
     }
?>

Using this template on the shop page...

<h2><perch:content id="title" type="text" label="Product title" required="true" title="true" suppress="true" /></h2>

<perch:repeater id="images" label="Fabric" scope-parent="true">
    <h2><perch:content id="parent.title" type="text" label="Product title" required="true" title="true" /> <perch:content id="title" type="text" label="Product title" required="true" title="true" /></h2>
    <perch:content id="desc" type="textarea" label="Description" textile="true" size="xs" help="Give some clear description of this product" />
    <img src="<perch:content type="image" id="image" label="Image" />"  alt="<perch:content type="text" id="alt" label="Image Description" />" />
</perch:repeater>

But this lists "all" contents of the repeater.

How do I get both s=list-detail&p=single-repeater-item to sort the template?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I can't see where you're passing p into the template. Are you doing that?

I ended up getting the "p" separately from the query string...

<?php $p = $_GET['p']; ?>

Then adding into Perch template...

<?php 
    PerchSystem::set_var('pitem', $p);
    perch_content_custom('My region', array(
        'template'=>'shop.html'
    ));
?>

And into the template to sort using if...

<perch:if id="pitem" match="eq" value="{slug}">

Is this the way I should be working with Perch?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, except it's safer to use perch_get() over $_GET.

Is it now working?

Yes the basic principle is working just needs a tidy up.

How do I use perch_get() ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Just change

<?php $p = $_GET['p']; ?>

to

<?php $p = perch_get('p'); ?>

Then it won't throw errors when p is not set, and it'll continue to work with routing if you upgrade to Runway in the future.

Fantastic. Thanks.