Panel templateChange hook

We have a hook called and it’s triggered if a page (folder) is renamed. We also have the hook which is triggered when a page is updated.

I’m missing a hook called something like which should be triggered if a template is changed (file is renamed).

At first it seems like a good idea to only have because changing a template is a kind of an update. The difference is that with a normal update it changes the content inside the file. When changing the template it rename the file. It’s two different operations.

We also have great methods that reflects the hooks:

But there is no function that I know of that renames the file on a template switch like $page->changeTemplate('from', 'to', $lang).

Plugins like and would benefit from a hook like that.

Hm, the issue is probably that the changeTemplate() method would then call two hooks, one when renaming the text file, and a second when updating the page content array?

I’m not sure how you mean. In the panel you can change the template or save the page, you can’t do both at the same time.

Now it works like this:

  • Change template =
  • Save page =

My suggestion:

  • Change template =
  • Save page =

Alternative suggestion

Because is triggered for two tasks and to not make a breaking change it would be possible to keep like it is but also make two subhooks available as an alternative:

  • Change template =
  • Save page =

But as I wrote, keep as well.

Well, what I meant was that if you have a look at the source code where the template changing happens, you will see that there are in fact two actions in it: first f::move to rename the file, then $page->update() to update the content.

I’ve tested it some more now in the panel.

If I write some content that I don’t save and then refresh the browser it says:

You have unsaved changes!

So far so good. It does not save my content because I did not press “Save”. I change my template, still with unsaved content.

Now it looks like it saved the data as well because the green smiley showed up and the message is gone. By looking at the title afterwards it seems to have saved the old content again, not the session/changed content. :upside_down_face:

My guess is that it destroys the session on template change and that the page updates to how the page was before the refresh, only to keep the page object updated.

In a plugin I’m working on I renamed a content textfile and then tried to get the new page template with page('a-page')->template() but it always showed me the old page object (template) before I renamed the file. I guess it uses some kind of cache. A page update triggered by the plugin there would probably solve the problem, just to keep the page object up to date.

(Many guesses in this post)


Personally I hope Kirby handles this a bit more elegantly in the future. Like keeping the session all the time until I press save and on change template, only change the template, not save the page.