Search

I've got a simple grid layout displaying products, 3 to a row and 4 rows. Each row is actually wrapped in a div with an id of 'row' in the grid:

<div class="row">
    <div class="product">
        ...
    </div>
</div> <!-- // .row -->
<div class="row">
    <div class="product">
        ...
    </div>
</div> <!-- // .row -->
<div class="row">
    <div class="product">
        ...
    </div>
</div> <!-- // .row -->
<div class="row">
    <div class="product">
        ...
    </div>
</div> <!-- // .row -->

I wasn't sure how to apply this in XSLT and currently I have:

<div class="row">
    <xsl:apply-templates select="/data/products-list-featured/entry[position() &lt;= 3]"/>
</div> <!-- // .row -->
<div class="row">
    <xsl:apply-templates select="/data/products-list-featured/entry[(position() &gt; 3 and position() &lt;= 6)]"/>
</div> <!-- // .row -->
<div class="row">
    <xsl:apply-templates select="/data/products-list-featured/entry[(position() &gt; 6 and position() &lt;= 9)]"/>
</div> <!-- // .row -->
<div class="row">
    <xsl:apply-templates select="/data/products-list-featured/entry[position() &gt; 9]"/>
</div> <!-- // .row -->

Can anyone recommend a cleverer way of doing this in XSLT? I'm sure there is a better way or doing it.

Something like this?

Shit. I'm too late (as always). Funny to see we both got the exact same solution, though:)

Edit: there is one thing to keep in mind: the xslt will keep adding rows when there are entries for it (say you aren't limiting to 12), nor will it create the rows, when there are no products to fill them.

Perfect! Thanks guys, it seems that approach gets the double thumbs-up. :) Plenty to learn from that snippet. I haven't used axes before and by the looks of things really need to take more advantage of XSLT modes. All very good to learn from.

Thanks.

Yeah, the "group n entries" usecase is a very good example for the use of apply-templates, modes and a few axis. Glad we could help!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details