Search

I have section appliances where each entry contains a name, type (fridge, oven, hob etc.) and image. Some types of appliances have a description. I need an appliances_view page that lists each entry for a particular type, e.g. appliances/view/ovens/.

For the items with a description (e.g. ovens) I would like to have a list style layout, whereas for entries without a description (e.g. ovens) I would like to have a grid layout, obviously the markup for each layout will differ.

My first thought would be to have an <xsl:if test="appliances/entry/description/node()"> to check if the first entry has a description, if so else call the grid template.

How does this sound? I have a lot of appliances to input so I don't want to do all of them and then realised I need to re-structure them!

Thanks

You can make use of "template overloading" (or "polymorphic behaviour"). Simply create two templates:

<xsl:template match="appliances/entry">
   [list style layout]
<xsl:template>

<xsl:template match="appliances/entry[description]">
   [grid style layout]
</xsl:template>

You can then apply them using

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

and XSLT will chose the appropriate template automatically.

That's so simple! Thanks phoque.

i didn't know about this. this is awesome.

That's a pretty neat way of achieving this! Is this sort of simliar to CSS in that it defaults to the template/style that is the most specific match?

Will have to try this out.

@phoque - Very nice!

Think I will have to revisit some of my xsl:if code. This is damn good.

Is this sort of simliar to CSS in that it defaults to the template/style that is the most specific match?

You can check out my article here on some different ways to override templates (i.e. using name, match, mode or a combination).

The summary of the article answers your question regarding matching order:

"Similar to CSS, priority resolution is in order of specificity. The more specific the matching pattern the higher its inherent priority."

@Allen - thanks - great article! I actually did read that when I was first getting started with Symphony/XSLT but most of it went over my head. Reading it again now I can see a LOT of ways I can improve my code.

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