Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Current »

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.

Input:

{{ attachment.crop }}

Output (no crop):

0,0,450,360

Output (horizontal crop):

0,100,450,200

Output (vertical crop):

120,0,300,360

You can use the crop values in the img_url filter like this:

{{ attachment | img_url: 600, crop: attachment.crop }}

attachment.src

Returns the original url of the attachment.

Input:

{{ post.featured_image.src }}

Output:

https://plate-attachments.s3.amazonaws.com/images/12ab34cd56/featured-image-path.png

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.

Input:

{{ attachment.meta }}
{{ attachment.meta.is_image }}
{{ attachment.meta.file_size }}
{{ attachment.meta.height }} x {{ attachment.meta.width }}

Output:

{"mime_type" => "image/jpeg", "file_size" => 12345, "ext" => "jpeg", "format" => "jpg"...
true
12345
450 x 360

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.

Input:

{{ post.featured_image.file_name }}

Output:

featured-image-path.png

attachment.content

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

Input:

{{ post.featured_image.content }}

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

  • No labels