Search

In my entry section I have a field called 'Priority' (a number field) which I'm using to allow the user to set a postion on the (HTML output) page. In this particular case it's a product grid with 12 items per page. The user wants to be able to set the position of each item on the first page with position 1 being top left and position 12 being bottom right.

In the datasource I'm pulling out the products and ordering by Priority in ascending order (1-12). However not every product with have a priority so this field could be empty. As a result this returns null which means that every product which doesn't have a priority set is listed above the ones that do no matter what value they have—since null will always preceed a number.

So, I'd like to know the best way to order by Priority so that 1 is always first and 12 is always last and anything without a value in this field is placed afterwards. Is this best done at the datasource level or in the XSLT? Any examples would also be useful.

Note: I can't filter by priority as I need ALL products returned rather than just those with a priority. Just to be clear.

I don't think it would be worth creating a custom PHP Datasource for that but rather sort it in the XSLT. What exactly does "null" look like in your XML?

The datasource is there to pull out the products anyway so it's not especially for the priority ordering but the ordering needs to be part of the display on the HTML page.

In the XML null doesn't actually output anything. So if a product has a value set in the priority field then that value is output in the XML as it's own XML node <priority>12</priority>.

In this case a first simple solution would be:

<xsl:apply-templates select="products/entry[priority]" />
<xsl:apply-templates select="products/entry[not(priority)]" />

instead of

<xsl:apply-templates select="products/entry" />

Well the XSLT way certainly looks simpler. Doh! :) Adding boolean functions and indexing to the list of things to keep in mind. Very clean and simple indeed.

Out of interest, is there a particular guideline where you would look to distinguish between filtering and prioritising in datasources or in XSLT? I think I had my mindset too far into the datasource method in this case but was just wandering if there were any particular guidelines on when it's better practice to use one or the other?

Usually in datasources I would try to filter out things I will not need on the page. Then I would use xslt in cases where I'd like to avoid using multiple datasources from the same section. This would be one of those cases.

You want to show 10 items, with the prioritized ones first. In the XSLT you just change the 'view' of stuff. Most of the time it depends on the use-case

Good point. In this case has I looked earlier I could have seen that the priority element was just left out of the XML if not present so it makes a perfect case for then sorting via XSLT. It's just a case of getting in the Symphony mindset which is getting there. :)

Thanks guys, much appreciated.

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