Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hi guys, I must've walked into a tree because I cannot for the life of me figure out how to check IF the current page has any related pages before cycling through them (So that it has div wrapping around it IF there are related pages, before then outputting them).

Code: Select all
<cms:pages id=k_page_id>
   <div class="tags">
      <cms:related_pages 'tags' >
          <a href="<cms:show k_page_link />"<cms:show k_page_title /></a>
      </cms:related_pages>
   </div>
</cms:pages>


This is the block of code where I list a posts related tags, but I want an if statement checking first if there are any tags to list, before showing the <div class="tags">.

Any help with my derp would be great! Thanks :)

EDIT: For clarification - the relation is defined as "<cms:editable name='tags' type='relation' masterpage='tag.php' label='Tags' />" within this template. (We're on the page-view here).
Image
Hi,

For such situations where the output depends on something that is yet to come in the execution flow, we use the cms:capture tag to buffer the contents and show (or discard) it later when the situation is clear -
Code: Select all
<cms:pages id=k_page_id>
    <cms:capture into='my_tags' scope='global'>
        <cms:related_pages 'tags' >
            <a href="<cms:show k_page_link />"<cms:show k_page_title /></a>
        </cms:related_pages>
    </cms:capture>
</cms:pages>

<cms:if my_tags ><div class="tags"><cms:show my_tags /></div></cms:if>

As an alternative, since your code involves cms:pages/cms:related_pages, you can use their 'count_only' parameter to first get the count of pages the tags will fetch and then proceed accordingly e.g. as follows -
Code: Select all
<cms:pages id=k_page_id>
    <cms:if "<cms:related_pages 'tags' count_only='1' />" >
        <div class="tags">
            <cms:related_pages 'tags' >
                <a href="<cms:show k_page_link />"<cms:show k_page_title /></a>
            </cms:related_pages>
        </div>
    </cms:if>
</cms:pages>

Choose whichever of the two solution suits you.

Hope it helps.
KK wrote: Hi,

For such situations where the output depends on something that is yet to come in the execution flow, we use the cms:capture tag to buffer the contents and show (or discard) it later when the situation is clear -
Code: Select all
<cms:pages id=k_page_id>
    <cms:capture into='my_tags' scope='global'>
        <cms:related_pages 'tags' >
            <a href="<cms:show k_page_link />"<cms:show k_page_title /></a>
        </cms:related_pages>
    </cms:capture>
</cms:pages>

<cms:if my_tags ><div class="tags"><cms:show my_tags /></div></cms:if>

As an alternative, since your code involves cms:pages/cms:related_pages, you can use their 'count_only' parameter to first get the count of pages the tags will fetch and then proceed accordingly e.g. as follows -
Code: Select all
<cms:pages id=k_page_id>
    <cms:if "<cms:related_pages 'tags' count_only='1' />" >
        <div class="tags">
            <cms:related_pages 'tags' >
                <a href="<cms:show k_page_link />"<cms:show k_page_title /></a>
            </cms:related_pages>
        </div>
    </cms:if>
</cms:pages>

Choose whichever of the two solution suits you.

Hope it helps.


Thankyou, KK.

Couch is much larger than most think and I often forget the use of some tags! Capture is the optimal solution here :)
Image
3 posts Page 1 of 1