Change template for existing content

I’m reworking an existing page and apparently when I built it initially I was too lazy to set up dedicated file blueprints.

Now I want to create an “images” section, but when I set template: image, the section contains nothing, because all files have the default blueprint. Any hints for creating a script to change all image files’ templates to image without losing existing content in their respective fields? I’m scared of losing content :smiley:

  1. Make a backup!
  2. Loop through all files (you can do this in a template/controller or a dedicated route.

Here is an example route:

            'pattern' => 'update-template',
            'action'  => function() {
                if ( ! kirby()->user() ) {
                foreach( site()->index()->images() as $file ) {
                        'template' => 'image'

If you have hundreds or thousands of files, you might run into a script runtime error, so make sure your PHP settings are fine or do it in chunks.


Thanks a lot, it worked like a charm! :dizzy:

If in the future I want to target only children of a specific page, would it work like

foreach(page('somepage')->index()->images() as $file)


Yes, exactly! Or you could filter by other criteria, like if a template is already assigned, by extension or whatever you feel up to.

1 Like

great maybe add to cookbook?


        'template' => 'event'

didnt work, but changeTemplate() did for me


I was logged in as an admin

Yes, it’s different for pages than for files, because for files the template is stored in the metadata file. For pages, however, the name of the content file is changed.

1 Like

got it