Write to field or file from a template

Hello :slight_smile:

Would it be possible or adviceable to write to a field OR a file from a template?

A site has all its image captions within structure fields.
This happens in several child pages under several child pages under pages.

As Kirby has a ‘caption’ field for files, they asked me to add it in the blueprint, then move all the captions from structure fields to this new caption field.

I was approaching this as a php batch file task, but then I realized that in a template I can easily get the old captions from any and all pages.

So maybe I can also copy them to the new caption field? OR alternatively open each text file from each image, and add it there , php batch file processing style just as I was doing before thinking about this?

Thank you

Yes, you can do that in a template or controller, no problem.

Check out: https://getkirby.com/docs/cheatsheet/file/update

Have you figured it out or do you need some more help?

I haven’t been able to try it yet, but I think your answer clearly solves my question. Thank you @texnixe!