Forum

Thread tagged as: Question

Check For Existence of Block

Is it possible to check for the existence of a block? For example:

article.html

<article<perch:if exists="image"> class="media"</perch:if>>
  <h2><perch:content id="title" type="text" label="Title" title="true" /></h2>
  <perch:blocks>
    <perch:block type="image" label="Image">
      <perch:template path="content/blocks/image.html" />
    </perch:block>
  </perch:blocks>
</article>

blocks/image.html

<img src="<perch:content id="image" type="image" label="Image" bucket="images" />" class="media__img" />

I'm using similar to the above but the "media" class never gets applied. Before switching to blocks I could test the image itself which would apply the appropriate styling, but it doesn't seem to work any more.

Thanks

Alan Longstaff

Alan Longstaff 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does <perch:showall /> give you? I don't think it's going to be available, but this is all new stuff.

This is the output:


ID Value perch_page_path /perch/core/apps/content/edit/index.php _id 884 title Article Title _title Article Title _blocks Array ( [0] => Array ( [text] => Array ( [raw] => Lorem ipsum dolor sit amet [processed] => Lorem ipsum dolor sit amet ) [_block_type] => text [_block_id] => nmwnrc [_block_index] => 0 ) [1] => Array ( [image] => Array ( [assetID] => 237 [title] => Chang [_default] => /perch/resources/images/chang.jpg [bucket] => images [path] => chang.jpg [size] => 124863 [w] => 488 [h] => 340 [mime] => image/jpeg [sizes] => Array ( [thumb] => Array ( [w] => 150 [h] => 104 [target_w] => 150 [target_h] => 150 [density] => 2 [path] => chang-thumb.jpg [size] => 5381 [mime] => [assetID] => 238 ) ) ) [alt] => [_block_type] => image [_block_id] => nmwnsq [_block_index] => 1 ) ) itemRowID 7967 itemID 884 regionID 631 pageID 242 itemRev 15 itemOrder 1000 itemJSON {"_id":"884","title":"Article Title","_title":"Article Title","_blocks":[{"text":{"raw":"Lorem{...} itemSearch Article Title Lorem ipsum dolor sit amet text nmwnrc 0 image nmwnsq 1 itemUpdated 2015-04-16 16:50:14 itemUpdatedBy 1 perch_item_first true perch_item_last true perch_item_zero_index 0 perch_item_index 1 perch_item_rev_index 1 perch_item_rev_zero_index 0 perch_item_odd perch_item_count 1
Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm, that's not ideal. That _blocks entry should ideally be flattened into something usable.

Nothing that helps now, but I'll log it and see what we can do for a future update.

Alright that's great thanks :)