File changeTemplate not working

hi everyone,

i am not sure if i am doing something wrong. i have a lot of older test images that have no template, hence technically default template i guess. i added this in the default file blueprint

options:
  changeTemplate: true

sadly it did nothing. i also tried the page approach (in the “file blueprint” docs it says either true/false, in the “page blueprint” docs it says false or list of possible templates)

options:
  changeTemplate:
    - image
    - video

also nothing. am i doing something wrong?

thx in advance!

(using kirby 4.2.0)

Just tested this in a Starterkit:

  1. Removed template entry from table.jpg.txt in about page
  2. Created new default.yml in site/blueprints/files
  3. Went to Panel and opened table.jpg
  4. Option to change template visible (sorry, language set to German, “Vorlage ändern”)

dear sonja,

thank you. hmmmm yes there it works, but still with a strange behaviour, maybe i am misunderstanding the feature. i created 2 extra templates, default and mytest. image template already exists. i deleted everything inside the fox picture (albums area of the starterkit). when i try to change it only shows default or image. why isnt mytest showing?

Bildschirmfoto 2024-04-23 um 20.18.35


Bildschirmfoto 2024-04-23 um 20.19.00
Bildschirmfoto 2024-04-23 um 20.20.01
Bildschirmfoto 2024-04-23 um 20.24.03

Bildschirmfoto 2024-04-23 um 20.20.26

and after i change it to image, i cannot change it back! so the feature only works when the template is empty? but then it should not be called “changeTemplate” but “assignTemplate” since it only works when the field is empty

ok, got something new. it seems to be dependant of the section the file is in.

for instance, this is a files section where i upload the files (in the parent page of the file):

  type: files
  headline:
    de: Bilder
    en: Images
  
  layout: cards
  template: image

  text: "{{ file.filename }}"
  empty:
    de: Noch keine Bilder.
    en: No images yet.
  help: max. 15MB

  image:
    back: white
    ratio: 4/3
    size: small

as you can see i have “template: image”. if i remove that part, it stops working again… the problem is the section can only have one template, correct? so it will always be default or the template defined by the section? or am i confusing myself somehow

sorry for the spam. it works indeed like i said now. but its not help at all, since

  1. if my section does not have “template: something” defined, i see all images, including default and image, etc

  2. but if template: image is not defined, then i cannot use changeTemplate inside the image

  3. if i add template: image to the section, and if i had the image open another browser tab, then i can change the template. but the section will not show default images of course for me to navigate through them and change them, because i just filtered it with template: image

hence i am not sure of the use-case for changeTemplate for files :confused:

That’s right. Kirby only allows you to change to a template that is used somewhere else in the page, otherwise, if there is no field or section that uses a given template, the image would “disappear”, i.e. it would continue to exist in your page, but without knowing the link to the file you wouldn’t be able to access it again. So you would either need a section that does not assign a template at all, or have multiple sections with different templates assigned, or have fields that use another template.

To just change the template of some images once, better do it programmatically.

thank you sonja. but again, i cannot change back. i just tested in another project. i have 3 sections, one for video, image and docs respectively. all the 3 file templates have changeTemplate:true. the files are all in the same page. i cannot change the templates. my point being, if i have a template “image.yml” and another “imagewithdetails.yml” you cannot change between them (please correct me if i am wrong). in case you manage to do this, could you please post a full example somehow?

thank you, yes i will change them with a global function now. i feel this topic needs a bit more explaining in the docs ^^ changeTemplate for pages is straight forward, and since the property is called the same but it works differently, i think it should be explained in detail.

Will double-check tonight and set up an example.

Ok, for a very simple usecase, I downloaded the Plainkit, then did the following:

  1. Created a new page blog/blog.txt in content
  2. Created a new blog.yml blueprint in /site/blueprints/pages with 3 files sections and one files field, all using a different template:
title: Blog

sections:
  filesA:
    type: files
    template: cover
  filesB:
    type: files
    template: image
  filesC:
    type: files
    template: pdf
  fields:
    fields:
      filesfield:
        type: files
        query: page.files.template('field-image')
        uploads: field-image

  1. Created 4 files blueprints cover.yml, image.yml and pdf.yml and field-image.yml in /site/blueprints/files. Apart from the different file name and title, they all have the same content, here cover.yml

cover.yml

title: Cover

options:
  changeTemplate: true
  1. I then uploaded files to each section and the field in that blog page.
  2. I can now click on a file to get to the file view, and when clicking on the cog icon, get the option to change the template.
  3. When I do that, I am presented with the following dialog:

Hope that helps.

dear sonja,

thank you! yes that is what i was expecting, to have the select have multiple options. at first glance seems similar to my test with the difference that i had mime types and other things. i will look at it in detail tomorrow.

i just tested it, and yes it works! and now i know also that indeed, the system checks the mime type. as soon as i put a

accept:
  type: video

or things like that, the template disappears from some lists. this makes sense of course. so now i tested with type image, video and document and also made a template-empty section in the blog.yml

files_no_template:
    type: files

and now the files that have no template can be changed to the corresponding template that allows the file type.

i tested all this in the starter kit though, now i have to find the solution in my actual project because i think i already tested the exact same thing. will post later again

ok, tested already and now it works :slight_smile: my problem was i was using a subpage to store all media, but i was also displaying the sections in site, not only in that subpage, because that page is not listed anywhere for the user in the panel.

like you explained, i had to add a files section with no template defined, but to both, the site and the subpage

###site.yml

sections:
      defaultfiles:
        type: files
        parent: site.find('main-library')
      imgs:
        extends: sections/files-imgs
        parent: site.find('main-library')
      vids:
        extends: sections/files-vids
        parent: site.find('main-library')
      docs:
        extends: sections/files-docs
        parent: site.find('main-library')

###medialibrary.yml

sections:
      defaultfiles:
        type: files
      imgs:
        extends: sections/files-imgs
      vids:
        extends: sections/files-vids
      docs:
        extends: sections/files-docs

and now it works :slight_smile: i think yesterday i was only changing the site.yml and not the mainlibrary.yml as well. thank you again sonja

1 Like