by
KK » Wed Oct 10, 2012 7:29 pm
We can use a technique very often employed with cms:pages tag ('related_pages' tag is very closely related to it).
Suppose we have the following snippet
- Code: Select all
<cms:pages masterpage='something.php' >
<cms:show k_page_title />
</cms:pages>
If there are suppose 5 pages, as expected the loop above will repeat 5 times and show the 5 entries. However if suppose there are
no pages to fetch, the loop above will not execute even once.
We can use this behaviour to help us figure out if there are no pages to show.
What we do is, we set a global variable, say named 'pages_found' before we enter the pages loop
- Code: Select all
<cms:set pages_found = '0' 'global'/>
<cms:pages masterpage='something.php' >
<cms:show k_page_title />
</cms:pages>
Then within the pages loop, we set this variable to '1'
- Code: Select all
<cms:set pages_found = '0' 'global'/>
<cms:pages masterpage='something.php' >
<cms:show k_page_title />
<cms:set pages_found = '1' 'global'/>
</cms:pages>
Obviously, if there are no pages to fetch, the 'pages' tag will not execute and the 'pages_found' variable will never be set to '1' i.e. will remain on its initial value of '0'.
We check this after the pages tag and if the 'pages_found' variable is still '0', we display the 'No pages found' message
- Code: Select all
<cms:set pages_found = '0' 'global'/>
<cms:pages masterpage='something.php' >
<cms:show k_page_title />
<cms:set pages_found = '1' 'global'/>
</cms:pages>
<cms:if pages_found = '0' >
No pages found
</cms:if>
The technique outlines above can be easily ported to 'related_pages'. An example could be -
- Code: Select all
<cms:set pages_found = '0' 'global'/>
<cms:related_pages 'artist_albums' >
<!-- All variables of 'albums.php' are available here -->
<cms:show k_page_title /><br/>
<cms:set pages_found = '1' 'global'/>
</cms:related_pages>
<cms:if pages_found = '0' >
No News Posts for <cms:show k_page_title />
</cms:if>
Hope this helps.