Kirby templates 101
In this post you are going to learn the basics of how to write clean templates with Kirby's PHP template engine.
Keep your code tidy
This is not only a rule for Kirby templates, but for all the HTML code you write – always keep it tidy, consistent and polished. I know it's often difficult with tight budgets or when you just started to learn how to code. But the cleaner you keep your templates right from the beginning, the easier it will be to maintain those later.
Get a good editor with great syntax highlighting
It's vital to feel comfortable with the editor and its syntax highlighting. It doesn't matter if it's Microsoft's Code, GitHub's Atom, an IDE like PHPStorm or something like SublimeText. Take what fits you.
Don't be afraid of PHP
I know that PHP looks a bit scary in the beginning, when you come from front-end developer background. But it's far less complicated than you might think. Here are the basics:
Display a variable
If clauses
If / else
foreach loops
Those are the most basic tools you will need in your templates.
That wasn't too hard, right?
So let's move on with some rules to write beautiful PHP.
Indentation
Always make sure to use proper indentation. It doesn't really matter if you use spaces or tabs to indent your lines, just make sure it's tidy. Considering PHP blocks as HTML blocks makes it easier to structure templates and makes them look cleaner in the end.
Don't use curly brackets
Bad
This becomes an unreadable mess when you have more than just one nested condition or loop and lots of html between it. Better use alternative syntax, which will help you to understand where blocks start and end.
Good
Never ever create entire HTML parts with PHP
Bad
This will always end up in unreadable Spaghetti code.
It might be ok to do that inside a function sometimes. But most of the times you really should avoid this.
Good
Use <?=
instead of echo
Since PHP 5.4 the echo "short tag" is permanently enabled, so you can use
<?=
instead of <?php echo
in your templates without having to make any php.ini or .htaccess settings.
There's also a short PHP tag (<?
instead of <?php
), which you can enable in your php.ini
or your .htaccess
file. However, we do not recommend using this short tag to keep your code as compatible and portable as possible.
By using the short echo
tag in conjunction with the standard PHP opening tag (<?php
), your templates become very readable and you can visually distinguish a control structure from an echo
easily.
Use snippets
Kirby has built-in includes for your templates called snippets. In other systems they are often called partials or stubs or something similar. There's an entire page in the docs about them, which you should definitely read.
Snippets can simplify your templates drastically, which will lead to better template readability.
No:
Yes:
Using snippets makes entire parts of your templates reusable for other templates and reduces the amount of code per template.
Use the js() and css() helpers
Kirby has a lot of built-in helpers to simplify your templates.
To simplify loading css and javascript files, you can use the following helpers:
Use the url() function
Working with absolute URLs in templates is a nightmare. That's why Kirby has the url() function to help you build correct links:
Bad
Good
Simplify your if clauses
In many situations you don't have to write a full if clause in your templates. There are shorter versions, which you should know about:
Bad
Good
Even simpler: Use the e()
function
A typical problem when writing templates is to switch on HTML attributes on demand.
(ie. for an active class on menu items)
Kirby has a built in e()
function, which works like a compressed if/else clause:
In the example above, class="home"
will only be added to the body tag, when the current page is the home page.
This even works with alternatives, when the condition is not matched:
This will apply the home
selector to the home page and the other
selector whenever the current page is not the home page.
And one last trick:
You often have to check for an existing object first, before you can use it. For example when you want to find out if an image exists, before you render an image tag.
Bad
Good
Still no friend of PHP templates?
We keep our template engine as simple and effective as possible, but we also don't want to lock you in. Our plugin system offers a simple way to embed your own template engine and there are plugins for Twig and other template engines available.