XSLT for Kirby 3

We work as designers, and over the years we have fallen in love with XSLT for templating. XSLT allows you to transform XML to any other structure by applying templates and thus you can use it to generate HTML from structured data. The syntax looks like HTML and works like CSS conceptually.

In order to use XSLT with Kirby, we created a plugin providing the needed template component:

The plugin automatically creates an XML layer for each page that can be customized for each page individually that can be accessed by appending ?data to the current URL, see https://github.com/hananils/kirby-xslt#data.

This layer can then be used to apply XSL templates, see https://github.com/hananils/kirby-xslt#templates.

6 Likes

I haven’t quite gotten myself the benefits of XSLT (I don’t doubt there are), but this looks great! Like the dark mode style!

Thank you! The layout still needs updating, it still includes ideas from the early Kirby 3 alphas and betas.

Regarding XSLT, from my experience over the years: you are either in love with it or you hate it. It’s not a solution for everything and everyone and it’s not without flaws. The biggest problem is validity: write something invalid and the processor throws an exception. Namespaces are hard as well.

Personally, what I love about it:

  • Given the default content structure the plugin provides, I don’t have to touch PHP. PHP is not my area of expertise, so leaving it behind is something I enjoy.
  • XSLT is a standardized, matured language that focusses on only a handful functions. But these functions are very powerful, like <xsl:apply-templates /> that you can use to convert any markup into the one you need. Think of a text written in Markdown: you can take it into pieces and create anything you like from it.
  • XSLT uses xPath to select data and xPath is so powerful when you’d like to find and fetch content across the whole document.

Most people I know hate this fact, but XSLT is very verbose when it comes to naming functions. I always liked this because it helps me to understand what a function does.

We just released an updated version with improved data and error layout. It also adds xPath filtering and translations: https://github.com/hananils/kirby-xslt/releases/tag/1.2.0

Data view

xPath matching

Error handling

1 Like

We just released version 2.0 with improved attribute handling and better performance:

1 Like

We updated our XSLT Kit for Kirby which bundles utility templates we often use in our projects:

Requirements

The kit requires the EXSLT extension, http://exslt.org/.

Namespace

All templates are namespaced, please add the following to your stylesheet:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:kit="https://hananils.de/xslt/kit">

Included templates and modes

All templates can be applied using <xsl:apply-templates /> using the matching mode:

  • kit:transform: If you apply this mode to a HTML structure, nothing changes in the output by default. But this mode allows you to offset the headline hierarchy, e. g. changing a h1 to a h3, or to match templates to elements allowing custom transforms. This is very helpful when you are dealing with HTML sources generated using Markdown. For more information see http://www.getsymphony.com/learn/articles/view/html-ninja-technique/.
  • kit:dates: Formats Kirby XSLT date nodes to a human readable date.
  • kit:dates-time: Formats Kirby XSLT date nodes to a human readabel time.
  • kit:dates-range: Creates human readable date and time ranges from a Kirby XSLT date node.
  • kit:dates-years: Creates human readable year ranges from a node set containing year values.
  • kit:list: Converts node sets to list, e. g. using a comma separator.
  • kit:links: Creates human friendly links.
  • kit:email: Creates a HTML link from a textual e-mail node.
  • kit:name: Formats a name with title, prefix and suffix.

There is also a language template containing information about the current Kirby content language as well as translation strings.