Forum

Thread tagged as: Question, Problem

Range of perch:every

Is it possible to select a range of items with perch:every?

I want to output 200 characters from a textarea in the first 3 items (of 9), then 60 from the next 6. More info for the most prominent 3 items, basically.

Following the docs here https://docs.grabaperch.com/docs/templates/conditionals/every, I'm using perch:every for each of the 9 items like this:

<perch:every nth-child="1">
  <perch:content id="desc" type="textarea" striptags="true" chars="200" append="…" />
</perch:every>
<perch:every nth-child="2">
  <perch:content id="desc" type="textarea" striptags="true" chars="200" append="…" />
</perch:every>
<perch:every nth-child="3">
  <perch:content id="desc" type="textarea" striptags="true" chars="200" append="…" />
</perch:every>
<perch:every nth-child="4">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>
<perch:every nth-child="5">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>
<perch:every nth-child="6">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>
<perch:every nth-child="7">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>
<perch:every nth-child="8">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>
<perch:every nth-child="9">
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>

There will only ever be 7 items, so from that perspective the template's solid enough, but it doesn't feel particularly efficient, but I can't seem to get it working any other way. Tried perch:else to take care of the remainders, which works fine when I'm targeting only one item like this:

<perch:every nth-child="1">
  <perch:content id="desc" type="textarea" striptags="true" chars="200" append="…" />
<perch:else />
  <perch:content id="desc" type="textarea" striptags="true" chars="60" append="…" />
</perch:every>

Tried comma separating the values in the nth-child attribute, tried adding more than one nth-child attribute, but neither works.

Maybe I'm getting the syntax wrong, maybe it's not something that has been built into Perch yet or maybe there's a way of doing it that I haven't considered.

Thanks in advance for keeping me right :)

Martin.

Martin Underhill

Martin Underhill 5 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think nth-child="-n+3" should give you the first 3.

This is really useful: https://nth-test.com

Hi Drew, that works a treat – thank you very much :)

And that testing resource looks really useful!