Post-processing Page-Output

Hi all,

looking for a way to post-process the generated frontend-output.
If possible, before the content gets cached.

The actual post-processing (code) is not the problem.
I’ve temporarily solved it directly in the index.php.
It gets executed there after cache-read, so it’s not ideal anyway.

I have no idea where to implement a call to that the “kirby-way”.
I’m sure there is a hook/event/etc. or something for it.

Any hints?

You could overwrite the $page->render() method in a page model:

Hi @texnixe,

thanks for the hint. It works, great success!

One part currently looks like this:

'pageModels' => [
  'default' => 'MyPageModel',
  'home' => 'MyPageModel',

Basically I want to extend all pages,
any way to add a wildcard for all page models?

No, I’m afraid there isn’t, you would have to spell them all out. Assigning the same model to all page types is already a shortcut. It doesn’t look as if there was an easy way to create a custom page class (ie. directly extend the Page class without the model), because the Page class is too deeply interwoven.

Do you really need this for all page types? What exactly is your use case for post-processing page output?

Alright, no problem then.
Your help is greatly appreciated.

Basically I can’t stand the output code. It’s a pain in the eye for me.
It has weird indentication, HTML-comments, empty lines, tags spread over multiple lines, etc.

I decided to go the route of clean, readable templates for comprehensible development.
Unfortunately PHP (as a templating language) does not reward it with adequate generated code.
So… I decided to not care about how I need to template to make the generated code look good, but instead I take a code-hammer at the end.
Currently using PHP Tidy for all mentioned above. If problems occur (markup, performance) it can be changed in one place (now).

Would an Add-On for your editor be an option? I´m using Format HTML in PHP (sometimes) for VS Code.

hi @Manuel,

I think in open collaboration it would cause problems.
Thanks for the suggestion though.

I wonder if the approach used in the kirby-minify-html plugin could work for you. I am not quite sure how the plugin hooking into the ‘template’ config compares to the Page Model approach outlined above, including the wish for it to happen pre-caching…

It sounds like your desire to clean up the final HTML code is very similar to the strategy of HTML minification. Maybe this discussion is of interest to you?

Hi @sebastiangreger,

Yes! You are absolutely right. Your links were my starting point some weeks ago :slight_smile:
I have previously installed this plugin and a previous kirby-installation has it still running.

Now, since I’m digging deeper in Kirby (it becomes my main CMS after all?), I am fine-tuning the processes/code-basis for the projects-needs.
At the moment I am using an own “default-project”-plugin-method to deliver the desired output. Seems to work for now. To be continued :slight_smile: