how to use kirby attachments

Mastering Kirby Attachments: Your Ultimate Guide to Using Them

By Amanda Martinez
AD

Looking to dive into Kirby CMS and make the most of its attachment functionality? You're in the right place. Kirby's attachments feature plays a pivotal role in managing your media files and data effortlessly. In this comprehensive guide, we'll explore how to use Kirby attachments, ensuring you understand their capabilities and functionality inside out.

Before we dive into the specifics, let's briefly understand what Kirby attachments are. Kirby CMS treats attachments as regular files, allowing you to store and serve various types of media - from images and documents to videos, and even zip files. They are incredibly versatile, making Kirby a robust choice for managing all your digital assets.

Uploading and Managing Attachments

Kirby makes it incredibly easy to upload and manage attachments.

AD
How To Use Kirby Vacuum Tools Attachments
How To Use Kirby Vacuum Tools Attachments
Miscellaneous Kirby Vacuum Attachment Functions Tips & Tricks
Miscellaneous Kirby Vacuum Attachment Functions Tips & Tricks
How To Use A Kirby Vacuum Zipp Brush Attachment Sentria Zip
How To Use A Kirby Vacuum Zipp Brush Attachment Sentria Zip

First, let's discuss uploading files. Kirby CMS provides a user-friendly interface for uploading attachments directly from your page files. To upload a file, simply drag and drop, or click and select the file from your system. Kirby automatically saves the original and creates thumbnails if the file is an image.

Basic Directory Structure

Understanding Kirby's directory structure is crucial for efficient file management. Files are stored in the `site/attachments` directory in your project root. Here, each file is a direct child of the `attachments` directory. Kirby assigns an ID to every file and uses it to reference the attachment in templates.

How To Use Your Kirby Vacuum Hose Attachments
How To Use Your Kirby Vacuum Hose Attachments
Kirby Cleaner in Progress Part 1
Kirby Cleaner in Progress Part 1

So, if you upload an image named `example.jpg`, Kirby generates a unique ID and situates the image in `site/attachments/example.jpg`. You can reference this image in your templates as `example.jpg` or `example` (Kirby automatically understands the file type).

File Fieldtype

Kirby's File fieldtype is another powerful tool for handling attachments. You can add it to your content models to create custom file fields. This feature comes particularly handy when you need to attach specific files to a page or a piece of content.

AD

To create a File field, add it to your fields array in the content model. For instance, if you're creating a new `text` field in your `homepage.yaml` file, you might include something like this:

``` fields: text: text image: files ```

This tells Kirby to add a 'image' field of type 'files' to the homepage. Now, you can upload any file to this field, making it a convenient way to attach media to your content.

Using Attachments in Templates

You've successfully uploaded and managed your attachments. Now, let's explore how to utilize them in your templates.

AD

Referencing attachments in your template is straightforward. You can use the `assets()` method provided by Kirby. Here's a basic example:

``` ```

In this case, `example.jpg` corresponds to the file ID Kirby has assigned to your uploaded image. The `assets::url()` method returns the URL path to the file, allowing you to display it in your template.

Generating URLs for Various Sizes

Kirby allows you to specify different image sizes for thumbnails. Say you want to display a resized version of your image in your template. You can use the following code:

AD
``` ```

Here, `300` and `200` are the width and height parameters, respectively. Kirby automatically generates a thumbnail in the specified dimensions and uses it in your template, preserving image quality.

Kirby attachments offer a world of creative possibilities. From managing media files to presenting them beautifully in your templates, Kirby's attachment functionality is a game-changer. As you continue your journey with Kirby CMS, remember this guide - it's your key to unlocking Kirby's attachment potential.

AD