Forum

Thread tagged as: Question, Runway

Checking for the existence of a block

Is it possible to check for the existence of a block with an if else statement?

I have a number of blocks with possible content types, one of which is embedded video. If there is no embedded video, I want the article to show a main image both on the listing page and at the head of the article. But if there is an embedded video, I want the image to show only on the listing page, not the article with the video.

Therefore I want something like 'if block video exists, do this. If not, do that'

Using Runway if that makes any difference (which I'm guessing it doesn't).

Thanks in advance

Mallen Baker

Mallen Baker 0 points

  • 5 years ago

I've tried a workaround to this, which should logically work, but doesn't!

I now have it that I have a lead image. The listing page has a <perch:if> clause that checks for the existence of this, and renders it if it is present.

Then I have the block that contains the embedded video. I've added a lead image field to this, which is suppressed on the article page, but not on the listing page. This also has an if statement to check for its existence. There are other blocks on the article page, but they are not needed on the listing page.

The code for the listing template looks like this.

                <article class="listing-feature">
                    <div class="listing-feature-image">
                    <perch:if exists="leadimage">
                        <img src="<perch:content id="leadimage" type="image" label="Lead image" height="335" width="710" crop="true" />" alt="<perch:content id="alt" type="text" label="alt text" />" />
                    <perch:if />
                    <perch:blocks>
                        <perch:block label="Video" type="video">
                            <perch:if exists="vid_thumb">
                            <img src="<perch:content id="vid_thumb" type="image" label="video screenshot" height="335" width="710" crop="true" />" alt="<perch:content id="vid_alt" type="text" label="alt text" />" />
                            </perch:if>
                    </perch:block>
                    </perch:blocks>
                    </div>
                    <div class="listing-feature-description">
                        <h2><perch:content id="title" type="text" label="Title" /></h2>
                        <div class="post-date">
                            <meta content="<perch:content id="date" type="date" label="Date" format="c" />" itemprop="datePublished" />
                            <time datetime="<perch:content id="date" type="date" label="Date" format="c" />"><perch:content id="date" type="date" label="Date" format="%d %B %Y" /></time>
                        </div>
                        <perch:content id="promo_para" type="textarea" size="s" label="Promo first para" editor="tinymce" html="true" />
                        <p><a href="/article/<perch:categories id="section" label="Category" set="section" ><perch:category id="catSlug" /></perch:categories>/<perch:content id="slug" type="slug" />/" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>

I can't see why that wouldn't work, but the output it renders is weird. It is all OK until it renders the video placeholder image - at which point it repeats all of the html without any database content, before correctly rendering the remaining content. And any subsequent entries in the list don't show, but some more empty entries show. The HTML it generates looks like this:

<article class="listing-feature"> <div class="listing-feature-image">

                            <img src="/perch/resources/julieglenn-w710h335.jpg" alt="This is Julie Glenn" />


                                        <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
                    <div class="listing-feature-description">
                        <h2></h2>
                        <div class="post-date">
                            <meta content="" itemprop="datePublished" />
                            <time datetime=""></time>
                        </div>

                        <p><a href="/article///" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>


                    </div>
                    <div class="listing-feature-description">
                        <h2>Julie Glenn talking about something</h2>
                        <div class="post-date">
                            <meta content="2016-01-23T00:00:00+00:00" itemprop="datePublished" />
                            <time datetime="2016-01-23T00:00:00+00:00">23 January 2016</time>
                        </div>
                        <p>This is a video of Julie Glenn doing a bunch of stuff many years ago on a talk show.</p>
                        <p><a href="/article/inspiring-people/julie-glenn-talking-about-something/" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>
                <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
                    <div class="listing-feature-description">
                        <h2></h2>
                        <div class="post-date">
                            <meta content="" itemprop="datePublished" />
                            <time datetime=""></time>
                        </div>

                        <p><a href="/article///" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>
                <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
                    <div class="listing-feature-description">
                        <h2></h2>
                        <div class="post-date">
                            <meta content="" itemprop="datePublished" />
                            <time datetime=""></time>
                        </div>

                        <p><a href="/article///" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>
                <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
                    <div class="listing-feature-description">
                        <h2></h2>
                        <div class="post-date">
                            <meta content="" itemprop="datePublished" />
                            <time datetime=""></time>
                        </div>

                        <p><a href="/article///" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>
                <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
                    <div class="listing-feature-description">
                        <h2></h2>
                        <div class="post-date">
                            <meta content="" itemprop="datePublished" />
                            <time datetime=""></time>
                        </div>

                        <p><a href="/article///" class="morelink">Read More &rarr;</a></p>
                    </div>
                <div class="clear"></div>
                </article>
                <article class="listing-feature">
                    <div class="listing-feature-image">



                    </div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Check your syntax - it looks like you've got a <perch:if /> there, which I think you meant to be </perch:if>

Well spotted, but I've corrected that and it's still doing the same thing, albeit there are now two images rendered rather than just the one.

Never mind - I've decided to use a work-around (pulling the video field out of the blocks). Thanks for the help anyway.