Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
Hello, a question on repeatable regions and the value in k_total_records.

I have a clonable template displaying a gallery which is set up as a repeatable region.

Within the <cms:pages> tag I am showing each cloned page including the gallery. For my second cloned page there is currently no gallery - so I don't want anything to display (including the <ul> tag, set up outside the <cms:show_repeatable> tag).

I expected to be able to use k_total_records to detect whether anything was set up for the repeatable region (i.e. check that k_total_records is 0). But regardless of whether startcount is set to 0 or 1, the value of k_total_records is = 1 for the 2nd cloned page's repeatable region.

I can stop the <li> elements being displayed by checking if the thumbnail is there or not, but I would like to be able to do a pre-display check of the repeatable region to test for nothing being present and stop the <ul> tag - and thought k_total_records could do this.
Code: Select all
<ul>
   <cms:show_repeatable 'book_gallery'  startcount='0' >
      <cms:show k_count /> / <cms:show k_total_records /><br/>         
      <cms:if k_count lt '6'>
         <cms:if thumbnail_book_img ><li><img src="<cms:show thumbnail_book_img />" alt="<cms:show k_page_title />" /></li></cms:if>
      </cms:if>
      
   </cms:show_repeatable>
</ul>

k-count and k-total-records from the above code displays "0 / 1" even though there are no entries in the repeatable region.

I wonder if you could enlighten me? Thanks!
Hi,

This is what is happening -
The repeatable regions tag, in a bid to lend a helping hand, automatically creates an empty row if there are no rows present. Upon saving the page, this automatically inserted row becomes the first (albeit empty) row of the table.

We could term this as an anomaly. Maybe I should fix this (I'd appreciate feedback from users).

For now, we'll have to work around this behavior.
There can be several ways of doing it but the cleanest that comes to my mind is using the cms:capture tag (you might recall reading about it in the documentation - http://www.couchcms.com/docs/tutorials/ ... -form.html. Section 'Buffering output').

The capture tag is meant exactly for the kind of 'pre-display check' you mentioned.
Suppose this is your original code:
Code: Select all
<ul>
   <cms:show_repeatable 'book_gallery' >
      <cms:if k_count lt '6'>
         <cms:if "<cms:not_empty thumbnail_book_img />">
            <li><img src="<cms:show thumbnail_book_img />" alt="<cms:show k_page_title />" /></li>
         </cms:if>
      </cms:if>
   </cms:show_repeatable>
</ul>

The problem with it, as you mentioned, is that the UL tags will get outputted even if there are no LI rows that get outputted (maybe because there are 0 rows or maybe because there are no suitable rows).

Instead of outputting markup directly as above, we now capture the output into a buffer by simply enclosing the code within the cms:capture tags. Our code now becomes:
Code: Select all
<cms:capture into='my_buffer'>
<ul>
   <cms:show_repeatable 'book_gallery' >
      <cms:if k_count lt '6'>
         <cms:if "<cms:not_empty thumbnail_book_img />">
            <li><img src="<cms:show thumbnail_book_img />" alt="<cms:show k_page_title />" /></li>
         </cms:if>
      </cms:if>
   </cms:show_repeatable>
</ul>
</cms:capture>

The output of the code gets buffered in 'my_buffer' variable.
We can now choose either to show the variable or to simply ignore it.
Since we wish to display the markup only when at least one LI row has been generated, we set up a flag when we do so and then we display the buffer only if the flag is set -
Code: Select all
<cms:capture into='my_buffer'>
<ul>
   <cms:show_repeatable 'book_gallery' >
      <cms:if k_count lt '6'>
         <cms:if "<cms:not_empty thumbnail_book_img />">
            <li><img src="<cms:show thumbnail_book_img />" alt="<cms:show k_page_title />" /></li>
            <cms:set show_buffer='1' 'global' />
         </cms:if>
      </cms:if>
   </cms:show_repeatable>
</ul>
</cms:capture>
<cms:if show_buffer ><cms:show my_buffer /></cms:if>

As you can see, if no LI is generated the flag never gets set and hence the entire output (along with the ULs) simply gets discarded.

Buffering output is a very helpful technique for make such 'pre-display decisions'.
Hope this helps.
hi ... thanks, that works - I just had to add:
Code: Select all
<cms:set show_buffer='0' />
above the opening cms:capture tag, otherwise the <ul></ul> tags were generated.

My feedback on the "anomaly" is it would be nice if the single empty row could co-exist with k_total_records = 0, but hardly essential, since there is an easy work around.

A QUESTION re your code: I was checking for the presence of a thumbnail as follows:
Code: Select all
<cms:if thumbnail_book_img >
and you changed this to
Code: Select all
<cms:if "<cms:not_empty thumbnail_book_img />">
. Could you explain what the difference is? My version seemed to work OK!


EDIT A SHORT WHILE LATER: I now seem to be getting erratic output with thumbnails missing when they exist ... I need to investigate further to see what is happening (tomorrow though, as it's now Saturday evening!).
. Could you explain what the difference is? My version seemed to work OK!

No difference actually in your case :)
It really matters only for richtext/nicedit types that have a penchant of inserting superfluous tags like <br />,<p> etc. even when the editor has no visible content. The cms:not_empty tag strips off such tags and then checks if the input is empty.

It is just that I've got into a habit of using this tag whenever I check for empty strings :)
4 posts Page 1 of 1
cron