We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Passing variables inside block templates

Hi there

I am trying out blocks. I's superb. But there's one thing I am having a block with brain-wise.

I have a bootstrap image carousel inside one block that has two loops using the same set of data (one displays the images, the other the indicators).

Previously I would have displayed this using perch_custom_content from a layout template and used two templates - one for each loop. But now the carousel is called from a block inside an html file. How do I pass variables from one html template to another? i.e. from the carousel image template to the carousel indicator template?

Am I being a totally numpty?

Jonathan Elliman

Jonathan Elliman 27 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm having trouble visualising the problem. What does your code look like?

Hi Drew

I hadn't actually coded anything! I've worked it out. It was a numpty thing. I've just repeated the repeater id for both list items in the template below.

The question now is if I have a checkbox "carousel_active" allowing images to be turned on and off, how do I get the second loop (for carousel indicators) to count the right number of items. At the moment it skips the item rather than counts through the number of items. i.e. 0,2,3 not 0,1,2 if item 2 is not active. This then breaks the clickable navigation on the indicators.

        <aside class="l-aside eight">
            <div id="myCarousel" class="carousel slide">

          <div class="carousel-inner">
          <perch:repeater id="carousel_items" label="Slideshow images"> 
                    <perch:if exists="carousel_active">             
                    <div class="item<perch:if id="perch_item_index" match="eq" value="1"> active</perch:if>">
                            <img src="<perch:content type="image" id="carousel_image" label="Carousel image" width="512" height="442" crop="true" density="2" help="The image should be cropped to 1024x884px" order="4" />" width="512" height="442" alt="<perch:content type="text" id="carousel_img_alt" label="Alt description" help="e.g. Photo of a product" order="5" />" />

                        <perch:content id="carousel_active" type="checkbox" label="Carousel item active" value="yes" suppress="true" order="6" />
                    </div>      
                    </perch:if>
            </perch:repeater>

<ol class="carousel-indicators">
                    <perch:repeater id="carousel_items" label="Slideshow images">
                        <perch:if exists="carousel_active"> 
                        <li data-target="#myCarousel" data-slide-to="<perch:content id="perch_item_zero_index" />"<perch:if id="perch_item_index" match="eq" value="1"> class="active"</perch:if>></li>
                        </perch:if>
                    </perch:repeater>
                </ol>

          </div>                        
        </aside>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm. I'm not sure I have an answer for that one. perch_item_index gives the index of the item in the set, but doesn't account for any you choose not to display.

Ok thanks Drew. I will take out the active function for now. It's not vital.

If I recall Expression Engine has a simple count tag that allows you to iterate through filtered entries without relying on the original array of data returned. Perhaps <perch:count> would be a useful future feature request for other Perchers out there?

Drew McLellan

Drew McLellan 2638 points
Perch Support

We are looking at adding some basic math features to the template engine, but I can't promise anything immediately.

Thanks Drew.