Module: DynamicImage::Helper::Pictures
- Included in:
- DynamicImage::Helper
- Defined in:
- lib/dynamic_image/helper/pictures.rb
Overview
DynamicImage Helper Pictures
Renders responsive images: a picture element with a WebP source covering a range of widths, and an img fallback in a format everything understands.
These helpers take an optional ratio instead of a size, leaving the width to the browser.
#dynamic_image_tag renders at a size you pick; these render across a range of widths.
Instance Method Summary collapse
-
#dynamic_picture(record_or_array, options = {}) ⇒ DynamicImage::Picture
Returns a Picture for the record, which is what the tag helpers render.
-
#dynamic_picture_source_tag(record_or_array, options = {}) ⇒ String?
Renders a single source element, for composing a picture by hand.
-
#dynamic_picture_tag(record_or_array, options = {}) ⇒ String
Renders a responsive picture element.
Instance Method Details
#dynamic_picture(record_or_array, options = {}) ⇒ DynamicImage::Picture
Returns a Picture for the record, which is what the tag helpers render.
26 27 28 |
# File 'lib/dynamic_image/helper/pictures.rb', line 26 def dynamic_picture(record_or_array, = {}) DynamicImage::Picture.new(self, record_or_array, (.symbolize_keys)) end |
#dynamic_picture_source_tag(record_or_array, options = {}) ⇒ String?
Renders a single source element, for composing a picture by hand.
Use it for different crops per media query: several sources, each with its own crop and query. The browser
takes the first one whose media matches and whose type it supports, so put the specific queries first
and the unconditional one last.
85 86 87 88 89 |
# File 'lib/dynamic_image/helper/pictures.rb', line 85 def dynamic_picture_source_tag(record_or_array, = {}) = .symbolize_keys picture_source_tag(dynamic_picture(record_or_array, ), [:media]) end |
#dynamic_picture_tag(record_or_array, options = {}) ⇒ String
Renders a responsive picture element.
The img carries the width and height of the fallback, which needn't match whichever candidate is
chosen.
Any other options are passed on to DynamicImage::Helper#dynamic_image_tag.
52 53 54 55 56 57 58 59 |
# File 'lib/dynamic_image/helper/pictures.rb', line 52 def dynamic_picture_tag(record_or_array, = {}) = .symbolize_keys picture = dynamic_picture(record_or_array, ) source = picture_source_tag(picture) unless picture.sources.empty? image = picture_fallback_tag(record_or_array, picture, ) tag.picture { safe_join([source, image].compact) } end |