Problems, need help? Have a tip or advice? Post it here.
11 posts Page 1 of 2
I have a globally defined repeatable region in globals.php, a set of customer reviews, which I will have to output throughout the website:

Code: Select all
<cms:repeatable name='client_reviews' label='Client Reviews' order='3' >
      <cms:editable name='client_name'
                    label='Client'
                             type='text' />

      <cms:editable name='client_review'
                        label='Review'
                         height='100'
                        type='textarea'/>
  </cms:repeatable>


I need to output the following HTML-markup:

Code: Select all
<div>
   <blockquote>
     <p>"This is a review..."</p>
     <footer><cite title="Source Title">Client Name</cite></footer>
    </blockquote>
</div>


I tried to display all available data sets within a JS-Carousel with the following code without success:

Code: Select all
<div id="carousel" class="no-js owl-carousel">

      <cms:get_custom_field 'client_reviews' masterpage='globals.php' >
           <div>
              <blockquote>
                  <p><cms:get_custom_field 'client_review' masterpage='globals.php' /></p>
                  <footer><cite title="Source Title"><cms:get_custom_field 'client_name' masterpage='globals.php' /> 
                  </cite></footer>
              </blockquote>
           </div>
       <cms:get_custom_field />

</div>


I get the following error message:

"ERROR! Tag "get_custom_field" has no matching closing tag"

How can you output a globally defined repeatable region?


Furthermore, is it possible to randomly output only 1 entry from this data-set (i.e. on page reload only one random entry is displayed)?
Repeatable regions aren't the same as editables. As you can see where you defined the repeatable it wraps around a bunch of other editables. Think of it like a mini version of cloned pages for editable regions on a page. You can't access it via the get_custom_field because it's not an editable region. To get the contents of a repeatable you must use <cms:show_repeatable> tag. Please refer to docs: http://www.couchcms.com/docs/tags-reference/show_repeatable.html however I am not sure if this tag supports the "masterpage" parameter that allows you to access it from other templates.

You could try it out, or perhaps KK will shed some light :)
Image
Hi,

As @Bartonsweb correctly pointed out, repeatable regions are a little different from the normal editable regions.

However, we can still access their data from anywhere in the site quite easily.
For example, if we place the following code in 'globals.php' template itself it'll display the data from the repeatable region
Code: Select all
<cms:show_repeatable 'client_reviews' >
    <cms:show client_name />
    <cms:show client_review />
</cms:show_repeatable>

To make the code above work on any other template, we'll need to bring 'globals.php' into context as follows -
Code: Select all
<cms:pages masterpage='globals.php' limit='1'>

    <cms:show_repeatable 'client_reviews' >
        <cms:show client_name />
        <cms:show client_review />
    </cms:show_repeat
   
</cms:pages>

So the trick is to use cms:pages tag to first fetch 'globals.php' and then execute the cms:show_repeatable block within its context.

Hope it helps.
Ok, I can output the data set using your code. Hovwever, the limit='1' tag is not working. It will always output the complete data set. Furthermore, is it possible to randomly display one entry, not always the latest one?

Code: Select all
<cms:pages masterpage='globals.php' limit='1'>
          <cms:show_repeatable 'client_reviews'>
              <p class="lead">"<cms:show client_review />" - <cms:show client_name /></p>           
          </cms:show_repeatable >
</cms:pages>
The limit='1' in the code applies to cms:pages tag (which makes it fetch only one page from globals.php) and not the cms:show_repeatable tag.

The cms:show_repeatable does not support a limit parameter and always fetches all the rows.

To display only one of the fetched rows requires a bit of work from your end.
This. incidentally, has been answered in one of your own thread -
viewtopic.php?f=4&t=9245

Randomizing the single row would require falling back on PHP to set the k_count being checked to a random value (not beyond the total number of rows).

Do you think you'd be able to do that or would you like me to help with it?
Try this:
Code: Select all
<cms:show_repeatable 'client_reviews'>
   <cms:if k_count = '1' >
      <p class="lead">"<cms:show client_review />" - <cms:show client_name /></p>
    </cms:if>         
</cms:show_repeatable >

If you put your customer reviews into their own separate clonable template then you could use orderby='random' to show a random entry rather than just the first. The repeatable tag has a more limited feature set than a clonable template. There's no built in way to choose a random item.

A separate clonable template might be a better choice for this situation anyway.
I have quite a nice way of handling reviews/testimonials ...

The review is a clonable template. On other pages I then set up a repeatable region for type of dropdown - the dropdown pulling in all the reviews (see http://www.couchcms.com/forum/viewtopic.php?f=4&t=8032&p=15209&hilit=dropdown+cloned+pages#p15209. This means the client can select which reviews they want to show on the page and can drag and drop to re-order (they love drag and drop!)

There is no element of randomness in this though, and it may not work that well for very large numbers of reviews. It depends if the client wants choice or not ...
Ok, got it working now with only one item fetched, in this case the first one:

Code: Select all
<cms:pages masterpage='globals.php' >
        <cms:show_repeatable 'client_reviews' >
           <cms:if k_count='1' >
              <p class="lead">"<cms:show client_review />" - <cms:show client_name /></p>   
            </cms:if>
         </cms:show_repeatable >
</cms:pages>


I know how to randomly echo an item from a PHP array. However, I am not sure in which format the repeatable region is saved:
Code: Select all
<?php $quote = array(
            '1' => "This is quote 1",
            '2' => "This is quote 2",
            '3' => "This is quote 3",
            '4' => "This is quote 4",
            '5' => "This is quote 5",
          );
        ?>

<p class="lead"><?php echo $quote[array_rand($quote)]; ?></p>


Would appreciate your help how to translate this to Couch.
The repeatable region has a variable k_total_records that holds... the total number of records. So you can generate a random number from 1 to k_total records, then show that entry. I think this will work:
Code: Select all
<cms:pages masterpage='globals.php' >
   <cms:show_repeatable 'client_reviews' >
      <cms:set random_number="<cms:php>echo rand(1, <cms:show k_total_records />);</cms:php>" scope='global' />
   </show_repeatable>
</cms:pages>

<cms:pages masterpage='globals.php' >
        <cms:show_repeatable 'client_reviews' >
           <cms:if k_count="<cms:show random_number />" >
              <p class="lead">"<cms:show client_review />" - <cms:show client_name /></p>   
            </cms:if>
         </cms:show_repeatable >
</cms:pages>
That worked. Thanks.
11 posts Page 1 of 2