I’m having some trouble understanding when to use Controllers and when to use Models in the context of a Kirby-theme. I’ve read the documentation, but I’m still not sure about the use-cases for both of them.
For example: I want to be able to change the symbol that seperates the page title and the site title in the html
<title> element. I decided to use an optional field in the site.txt (if there’s a better way to do stuff like that, feel free to tell me!):
Then I have a function that checks if this field is present and builds the correct
<title> based on that:
$page_title = $page->title();
$page_title .= ' ' . $site->title_seperator();
$page_title .= ' –'; //default seperator
$page_title .= ' ' . $site->title();
$page_title can then be passed to the header-snippet and used for the
<title>-tag. But where should this function reside? Seems like it would bloat the template. So do I use a controller or a model for that? Or do I put in directly into the snippet? I’m not used to the MVC-pattern, so normally I would just dump stuff like that in a functions.php file somewhere and include it in my templates, but I understand that’s not a good way to do it.
If you think this should go directly into the snippet, I would still greatly appreciate if someone could show me an example of what you’ve used controllers and models for in your own theme(s); I really want to learn the proper way to work with kirby. Thanks!
The main problem here is that controllers and page models are tied to templates (controllers) or pages (page models).
Snippets cannot have their own controllers or page models. Therefore, logic for your snippets either has to go into the snippet itself, into the page models/controllers of the pages that use that snippet, or into a plugin.
The difference of page models vs. controllers is scope.
While controllers are only accessible from the templates with the same name, the methods of page models are also accessible from other templates that reference the page as well.
@texnixe Thank you for the explanation! I will put my code in the snippet then. Still seems a bit counterintuitive though; would it be bad practice to put functions like this in an extra file within the snippets directory and include it in the snippets as needed?
@lukasbestle Thanks for the link, I think I get it now °v°
As a general rule, I would not put it into the snippets folder or if so, then only into a subfolder within snippets. I think the best place would indeed be the plugins folder or you could also put it into assets. If you wrap your code in a function and put it into /plugins, then your function is always accessible without the need to require it first.
Thank you, I did not know that! By the way, where can I find more information on how plugins work in Kirby/ how to build one? I can’t find anything on that topic in the documentation …
Well, there is no documentation really, because a plugin can be anything from a simple function to something really powerful like the patterns plugin. And also, as of now, there is no standardized way of creating a plugin.
As a starting point, check out existing plugins.
Also, see this topic.