Keep code dry
Modularizing your code into reusable chunks to keep things organized and not repeat yourself is an important coding paradigm.
However, what is true for the big picture, is also true for the smaller chunks of code we produce daily in our templates.
Here is a simple example of what happens quite often:
We define a variable in our controller and pass it to the template:
And then in the template:
What's wrong here? And why is this a problem? Although we defined the variable in the controller, we do not use it in the template, but perform the same query again as before in the controller.
This means that Kirby has to perform the same query twice instead of using the result stored in the $articles
variable.
Furthermore, this often results in unexpected behaviour. Consider this example:
If we now use the same template code as in the example above, we will probably be surprised why the filter doesn't work. So if this happens, check if you haven't made a new unfiltered query in the template.
Our template should therefore look like this:
Let's consider another example from our cookbook, the treemenu snippet in an extended version with many additional conditional attributes:
This is a rather simple example but if we look closely, the same two queries for $p->hasChildren()
and $p->depth()
are used over and over again. The fact that this treemenu snippet works recursively, i.e. the snippet calls itself again for each level, makes this even worse. If the queries were more performance relevant here, for example a more complex database query, this would quickly add up, and the result could be a serious performance issue that is aggravated with each level in the recursion.
We should therefore fix this and store the result of the queries in variables first, then replace the repeated queries with these variables:
Takeaway
Always store query results in variables when they are used multiple times. This improves your overall performance, keeps your code DRY and easier to read, and prevents unexpected results.