Forum for discussing general topics related to Couch.
2 posts Page 1 of 1
On my Articles page, I have a list of archived months which is growing rather large. I thought I would shorten it up by 50% by having every other one float left or right on the same line. This would work but I need to programatically set the style with php. This code doesn't work: it makes every div float:left. Why doesn't the if() statement see the value set in $style?
Code: Select all
<cms:archives masterpage='articles.php' >
   <cms:php>
      if ($style == 'float:left;clear:both') {
         $style =  'float:right;';
      } else {
         $style =  'float:left;clear:both';
      }
      echo '<div style="'.$style.'"><a href="<cms:show k_archive_link />"><cms:date k_archive_date format='F Y' /></a></div>';
   </cms:php>
</cms:archives >

First time through, the condition should be false, then alternate each time it's evaluated so I get a left, right, left, right....
There is no need to use PHP, native couch tags will do.

Code: Select all
 <cms:archives masterpage='articles.php' >
<div style="<cms:zebra 'float:left; clear:both;' 'float:right;'/>">
<a href="<cms:show k_archive_link />">
<cms:date k_archive_date format='F Y' />
</a>
</div>
</cms:archives >


http://docs.couchcms.com/tags-reference/zebra.html

The zebra tag alternates between the parameters applied. In this instance it will alternate between floating left/right.

For future reference, the reason that your PHP code did not work is that the variable $style is scoped incorrectly.

EDIT : You could use CSS features like Columns, Flexbox or grid to create the same effect without using CSS floats and without using couch/php at all. It would be responsive too
2 posts Page 1 of 1