Learn to build a lean & flexible blogging platform
A couple of chapters ago, we’ve had a closer look at Kirbytext - and we saw that it’s really a perfect mixture of plain text, Markdown syntax, and its own “Kirbytags”. It offers a host of elements: from lists to hyperlinks, from very flexible image tags to download links it covers most use cases.
However, there may come times when the specifics of your project demand for your own, custom Kirbytags. And luckily, Kirbytext is indeed extensible.
As an example, let’s write our own “button” tag. Our goal is that the we can use the following in our content:
( button:Read more link:posts/pencil class:red)
…and automatically and comfortably have it render to something like this:
In “attr”, all attribute names for this tag are defined. Kirby has to know what keywords to watch out for when parsing the tag contents; in our case, it needs to look for “button:”, “link:” and “class:”.
In “html”, we define a function that returns the final HTML that we want to output.
Defining Our “button” Tag
We’ll start by defining the tag and its attributes. The tag’s name (“button” in our case) is automatically available as an attribute and doesn’t have to be defined explicitly in the “attr” array: