General architectural questions about how to realize comments for a page


I want to provide a comment system for specific page types (blueprints) on my Kirby 3 site. I’m unsure if I should realize it by a subfolder/subdirectory with subpages (children) and if so, how to:

  1. Should I create a page model? (not sure because I don’t know what happens with variables within a page model; this is not documented)
  2. Is a pages field enough which gets updated by some http request if the comment form is submitted?
  3. Do I need some mix of these ways/parts of Kirby 3?
  4. Maybe I have to introduce sections?

I checked out two or three comment plugins but at the end, I’m not sure what’s the optimal way nor why they (partly) work as they do. I think if I know what’s the way to go, I will be able to go there but right now, I don’t see how to start because I don’t know how all of these things work out at the end and how they will result on file system level like what structure will they generate.

I expect the same kind of questions to appear if I would have to realize some sort of more complex data or information to be stored for a page on the file system.

Best Regards,

Before we get to the implementation, what do you expect your comment system to do? Only comments to an article, or comments to comments as well? What is the workflow for these comments, should they appear directly on the page or only when approved? What sort of spam protection do you want to implement, if any? etc. etc.

You might want to consider an external service as well:

Hi Sonja,

fair question! I didn’t think about comments to comments (threads). Right now, I would say that this doesn’t matter because it will be not too hard to implement if I understand the general concept. I don’t need any kind of login or comment rating. As a basic spam protection, I would do it like with statuses of pages: Either a comment is in draft status or published/public. I just delete unwanted comments before I publish them by hand.

An external service is good but I was afraid of privacy implications. Regardless of that, I have to understand how to achieve this basic feature in Kirby 3. I’m aware that I have to add some features by myself beside what you mentioned but that’s not too hard if it’s just one/several more fields per comment and can be managed like a page.

Well, basically, your comments would then be subpages of a (post) page or maybe even better in a separate comments subfolder.

  • You obviously need a form on the frontend, where users add the comment.
  • On submit, a new comment subpage is created (how to do that is explained in a cookbook recipe).

That’s basically all there is to it.

Of course, you probably want a blueprint for the single comment in the Panel, you need some code that displays the comments beneath the post once they are published.

1 Like

I will try this within the next days and I will update this thread with the result.

I wasn’t too sure a subpage is a good idea. Yep, a subfolder for the comments is something I thought about too.

You can also store you comments in a database…

A lightweight SQLite database would actually be very good solution. If I had to do it, I’d probably give that option a chance.

This original K2 plugin updated to K3 could be a starting point for your own adventure:

I don’t know if it is fully functional or if the issue it had with non-existing comments folders was ever resolved, but as I said, starting point.

Am I right that I have to create a new route and enable POST requests for it when I have a comment.php controller and I have to use it inside my HTML5 form:

<form class="comment-form" action="<?= $page->url() ?>" method="POST">

In my case the comment form is shown for every article of a specific template but of course the action URL is wrong; it should always link to my Kirby 3 controller, regardless of on which page I am. This is a bit different to the event example where you always have a dedicated page just for submitting events.

I’d put the logic into the page controller of the page type (i.e. the article controller) where the form is shown, doesn’t make sense to put it into the comment.php controller, I think. But you could also create a route that handles the form logic.

Ah, this is a good idea! I forgot that there’s always a controller for each blueprint/template . . . Now it works fine.

If I want to put my comments in a subfolder (as you described in your 2nd reply): Do I have to create a subpage called comments and then create each comment as a subpage of this subpage?

Yes, I think a comments folder makes more sense than having all comments as subpages. You could also auto-create the comments subfolder directly on page creation, that probably makes the most sense (even if that means that the folder could remain empty if there are no comments).

Oh and I just saw your comment.txt so I have to create a blueprint (which I wanted to do anyway because my article blueprint contains too menu irrelevant fields for a comment). So this is perfect as I will solve two todos with one blueprint!