Forum

Thread tagged as: Question

Get perch_item_count from other regions

Is there a way to get the perch_item_count for a region outside of the region itself.

I have a div that contains 2 perch regions, 'text' and 'gallery' - I wish to count the number of times in the gallery and add it as css to the parent div.

The perch_item_count returns 2, as it is counting the direct children (text and gallery), is there a way to specify what region the count is drawn from?

Tony Astley

Tony Astley 0 points

  • 5 years ago
Simon Clay

Simon Clay 127 points

Hi Tony, that should be possible. Can you show your page and template code?

<perch:block type="text_block_with_gallery" label="Text with Gallery">
            <div class="textBlock gallery <perch:content id="color" type="select" options="Blue|blue,White|white" label="Style" />">
                <div class="wrapper clearfix">

                    <div class="pageText">          
                        <perch:content id="text" type="textarea" label="Text" markdown="false" editor="redactor" html="true" />
                    </div><!--/.pageText-->

                    <perch:repeater id="images" label="Gallery Images" size="xs">

                    <perch:before>
                        <div class="pageGallery count<perch:content id="perch_item_count" />">
                            <ul>
                    </perch:before><li><img src="<perch:content type="image" id="image" label="Image" />" alt="<perch:content type="text" id="alt" label="Description" />" /></li><perch:after>
                            </ul>       
                        </div><!--/.pageGallery-->
                    </perch:after>

                    </perch:repeater>

                </div><!--/.wrapper-->      
            </div><!--/.textBlock-->
        </perch:block>

Ideally I want the count of the pageGallery to be interred into the class declaration of the parent textBlock e.g.:


<div class="textBlock gallery count<perch:content id="perch_item_count" (count from the pageGallery) />

Thanks

Simon Clay

Simon Clay 127 points

Hi, with a bit of a re-jig we can do it like this:

<perch:block type="text_block_with_gallery" label="Text with Gallery">
    <div class="textBlock gallery <perch:content id="color" type="select" options="Blue|blue,White|white" label="Style" order="1" />  count<perch:repeater id="images" label="Gallery Images" size="xs" order="3"><perch:before><perch:content id="perch_item_count" /></perch:before><perch:content type="image" id="image" label="Image" suppress="true" /><perch:content type="text" id="alt" label="Description" suppress="true" /></perch:repeater>">
        <div class="wrapper clearfix">

            <div class="pageText">          
                <perch:content id="text" type="textarea" label="Text" markdown="false" editor="redactor" html="true" order="2" />
            </div><!--/.pageText-->

            <perch:repeater id="images" label="Gallery Images" size="xs">

            <perch:before>
                <div class="pageGallery count<perch:content id="perch_item_count" />">
                    <ul>
            </perch:before><li><img src="<perch:content type="image" id="image" label="Image" />" alt="<perch:content type="text" id="alt" label="Description" />" /></li><perch:after>
                    </ul>       
                </div><!--/.pageGallery-->
            </perch:after>

            </perch:repeater>

        </div><!--/.wrapper-->      
    </div><!--/.textBlock-->
</perch:block>

Basically, we're calling the Repeater earlier in the template to get the count in the DIV class but suppressing the image and alt, then using the Repeater later in the template where we want to output the image gallery.

Awesome. Works perfectly.

Simon Clay

Simon Clay 127 points

Cool :)