Forum
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
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.
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">
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.