attachment

Table of contents

The attachment object is returned when an attribute of a media field is called. E.g. {{ post.featured_image }} (when featured_image is a media field) returns an attachment object.

attachment.crop

Returns the crop values of the attachment if it’s an image. The values are:left, top, width, height. By default the left and top are set to 0, and width and height to 100% of the image size, i.e. no crop. The attachment’s crop values can be changed by enabling inline crop in the img_tag filter.

{{ attachment.crop }}
Input
0,0,450,360
Output (no crop)
0,100,450,200
Output (horizontal crop)
120,0,300,360
Output (vertical crop)
{{ attachment | img_url: 600, crop: attachment.crop }}
You can use the crop values in the img_url filter like this

attachment.src

Returns the original url of the attachment.

{{ post.featured_image.src }}
Input
https://plate-attachments.s3.amazonaws.com/images/12ab34cd56/featured-image-path.png
Output

attachment.meta

Returns an object with the attachment’s meta information, i.e. file mime type, file size, image dimensions if attachment is an image, etc.

{{ attachment.meta }}
{{ attachment.meta.is_image }}
{{ attachment.meta.file_size }}
{{ attachment.meta.height }} x {{ attachment.meta.width }}
Input
{"mime_type" => "image/jpeg", "file_size" => 12345, "ext" => "jpeg", "format" => "jpg"...
true
12345
450 x 360
Output

The meta object has the following attributes for all files:

  • mime_type
  • file_size
  • ext (file extension)
  • alt (changeable when editing the file in the media library)
  • title (changeable when editing the file in the media library)

When the attachment is an image, it has the following attributes as well:

  • format (image type: jpg, png, etc)
  • width
  • height
  • aspect_ratio
  • landscape (returns true when image is in landscape)

attachment.file_name

Returns the file’s name, the part of the src url that represents the file.

{{ post.featured_image.file_name }}
Input
featured-image-path.png
Output

attachment.content

Returns the file’s content, but only if the file contains plain text and is readable.

{{ post.featured_image.content }}
Input

Files that are considered plain text and readable are:

  • Plain text files, like TXT, or code files like JSON, HTML, CSV, etc.
  • SVG image files

Need help?

Do you have any question which is not answered in this knowledge base? Contact us. We are here to help you.