Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
Hello, I have a question regarding multiple "related_pages" listing syntax.
In my main template I have more than one related fields. The code below currently gives me only the first related pages set called 'exhibited_works':
Code: Select all
<cms:pages masterpage='exhibitions.php'>
       <cms:related_pages 'exhibited_works' 'exhibited_paintings' 'exhibited_...'>
                 <cms:show k_page_title /><br/>
       </cms:related_pages>
</cms:pages>

Generally speaking, what I want is to be able to list all of them and eventually have some conditional statements at a later stage, e.g putting a link to detail page if there are any related records etc. Putting || or && gives me an error, so I suppose I'm wrong with the syntax.
Any help will be greatly appreciated, thanks in advance.
I believe the correct syntax for conditionals, that has worked for me is

<cms:if "(<cms:not_empty exhibited_works /> && <cms:not_empty exhibited_paintings />)" >

Do something!

</cms:if>
There are 10 types of people in this world ... Those who can count in binary and …
Hi Gali,

cms:related_pages tag can handle only one related field at a time.

If we wish to use it with multiple fields, we'll have to call it as many times.
For example, suppose your template has two relation fields - 'exhibited_works' and 'exhibited_paintings'. We can use cms:related_pages loop twice to list pages related by the two fields
Code: Select all
<cms:pages masterpage='exhibitions.php'>
    <h1><cms:show k_page_title /></h1>
   
    <!-- exhibited_works -->
    <h2>Related Exhibited Works</h2>
    <cms:related_pages 'exhibited_works'>
        <cms:show k_page_title /><br/>
    </cms:related_pages>

   
    <!-- exhibited_paintings -->
    <h2>Related Exhibited Paintings</h2>
    <cms:related_pages 'exhibited_paintings'>
        <cms:show k_page_title /><br/>
    </cms:related_pages>
 
</cms:pages>

If, as I gathered from your post, you wish to first confirm if there are any related pages before outputting something, we can use the 'count_only' parameter to get the count of related pages first and the conditionally output the section e.g.
Code: Select all
<cms:pages masterpage='exhibitions.php'>

    <!-- exhibited_works -->
    <cms:set count_exhibited_works="<cms:related_pages 'exhibited_works' count_only='1' />" />
    <cms:if count_exhibited_works >
        <h2>Related Exhibited Works</h2>
        <cms:related_pages 'exhibited_works'>
            <cms:show k_page_title /><br/>
        </cms:related_pages>
    </cms:if>
   
    <!-- exhibited_paintings -->
    <cms:set count_exhibited_paintings="<cms:related_pages 'exhibited_paintings' count_only='1' />" />
    <cms:if count_exhibited_paintings >
        <h2>Related Exhibited Paintings</h2>
        <cms:related_pages 'exhibited_paintings'>
            <cms:show k_page_title /><br/>
        </cms:related_pages>
    </cms:if>   
   
</cms:pages>

Does this help? Do let us know.
Thanks.
Thank you very much for the suggestions.
With listing related records now everything is OK.
For this particular example I wanted to know if there are any related records and show a link accordingly. So, implemented <cms:set /> with "count_only", then learned what happens with records through <cms:dump /> and put my conditional statement.. Probably a bit lame :).. but so far this one works for me perfectly:
Code: Select all
<cms:pages masterpage='exhibitions.php' order='exhibition_year' order='desc'>
    <cms:set exhibition_works="(<cms:related_pages field='my_field1' masterpage='exhibitions.php' count_only='1' /> && <cms:related_pages field='my_field2' masterpage='exhibitions.php' count_only='1' /> && <cms:related_pages field='my_field3' masterpage='exhibitions.php' count_only='1' />)" />
      <cms:if exhibition_works="(0 && 0 && 0)">
         <cms:show exhibition_year />&nbsp;&nbsp;<cms:show k_page_title /></cms:if><br />
      <cms:else />
        <cms:show exhibition_year />&nbsp;&nbsp;<a href="<cms:show k_page_link />"><cms:show k_page_title /></a><br />
      </cms:if>
</cms:pages>
Glad it helped.

I would have used the following conditional but feel free to use whatever works :)
Code: Select all
<cms:if exhibition_works >
    <cms:show exhibition_year />&nbsp;&nbsp;<a href="<cms:show k_page_link />"><cms:show k_page_title /></a><br />
<cms:else />
    <cms:show exhibition_year />&nbsp;&nbsp;<cms:show k_page_title /></cms:if><br />
</cms:if>
5 posts Page 1 of 1
cron