Forum

Thread tagged as: Question

Is there a way to get the id/number of an item in a multiple item region?

I am getting confused! I have a multiple item region which has a repeater to show a images in a gallery. So a new item is created to create a new gallery and the repeater add images to that gallery. I need each gallery to have a unique identifier, but the gallery title is not suitable - it must be one number/word.

I thought I could use perch_item_index but this seems to spit out a different number for each item in the repeater - I need an index for the galleries themselves not their content. How can I do this without asking the user to input their own?

Hope that makes sense! It's making my head hurt!

Lisa Morena

Lisa Morena 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to use _id

Good but that doesn't work on items within the repeater. Do I need to use PerchSystem::set_vars or something?

On that note, I am also having problems trying to show the first item in the repeater slightly differently from the rest (i.e. so I can have a 'header' thumbnail and title for the each gallery).

Drew McLellan

Drew McLellan 2638 points
Perch Support

Could you combine both the repeater item _id and the perch_item_index ?

Eugh, this is getting messy. Can't seem to get the numbering working at all. How can I pass the value of _id into the repeater?

Plus everything that I would usually try for showing the first item differently from the rest doesn't work for the repeaters as they all get shown in the form! So if I do an if statement for the first item and add in the image that was created in the same region bit outside of the repeater it gets shown on the form for each repeater rather than just being spat out ont he page like usual!

Going to start the template all over to see where I'm getting into a mess....

OK so I have figured out the whole showing the first repeater differently thing with a bit of lateral thinking. However still having issues with the id. _id does not work within the repeater. If I use perch_item_index then I get a different id for each repeater item which is not what I want. I want a different id for each item (it's a multiple item region) to be used across all the repeaters of that item.

So [gallery1] for all of the repeaters of the first item, [gallery2] for all of the repeaters for the second item, etc.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you brought it into scope?

By default, a repeater is a silo – like a mini region within the content item. It has its own content scope, and the template engine treats it as a distinct zone with its own before and after properties and new item counts.

Content from outside the repeater is out of scope. You can bring it into scope within the repeater using the scope-parent="true" attribute on the repeater tag.

To prevent ID clashes, the items from outside the repeater become parent.originalID within the repeater. So a field that is id="title" outside the repeater would be id="parent.title" when brought into scope within the repeater.

Oooh, didn't know about that. I knew you guys would have thought if a way around it! Will try it now.

Works a treat, you star! Thank you!