You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+101-135
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,23 @@
1
1
# jekyll-responsive-image
2
2
3
-
A [Jekyll](http://jekyllrb.com/) plugin and utility for automatically resizing images. Its intended use is for sites which want to display responsive images using something like[`srcset`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#Specifications) or [Imager.js](https://github.com/BBC-News/Imager.js/).
3
+
A [Jekyll](http://jekyllrb.com/) plugin for automatically resizing images. Fully configurable and unopinionated, jekyll-responsive-image allows you to display responsive images however you like: using [`<img srcset>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#attr-srcset), [`<picture>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture), or [Imager.js](https://github.com/BBC-News/Imager.js/).
Either add `jekyll-responsive-image` to your Gemfile, or run the following command to install the gem:
11
15
12
16
```
13
17
$ gem install jekyll-responsive-image
14
18
```
15
19
16
-
Then you can either add it to the `gems` section of your `_config.yml`:
20
+
Then you can either add `jekyll-responsive-image` to the `gems` section of your `_config.yml`:
17
21
18
22
```yaml
19
23
gems:
@@ -22,9 +26,100 @@ gems:
22
26
23
27
Or you can copy the contents of [`responsive_image.rb`](lib/jekyll-responsive-image.rb) into your `_plugins` directory.
24
28
25
-
## Configuration
29
+
### 2. Create an image template file
30
+
31
+
You will need to create a template in order to use the `responsive_image` and `responsive_image_block` tags. Normally the template lives in your `_includes/` directory. Not sure where to start? [Take a look at the sample templates](sample-templates).
32
+
33
+
For more advanced templates, see the [**Templates**](#templates) section below.
34
+
35
+
### 3. Configure the plugin
36
+
37
+
You **must** have a `responsive_image` block in your `_config.yml` for this plugin to work. The minimum required configuration is a `template` path:
38
+
39
+
```yaml
40
+
responsive_image:
41
+
template: _includes/responsive-image.html
42
+
```
43
+
44
+
For a list of all the available configuration options, see the [**All configuration options**](#all-configuration-options) section below.
45
+
46
+
## Usage
47
+
48
+
Replace your images with the `responsive_image` tag, specifying the path to the image in the `path` attribute.
49
+
50
+
```twig
51
+
{% responsive_image path: assets/my-file.jpg %}
52
+
```
53
+
54
+
You can override the template on a per-image basis by specifying the `template` attribute.
You can use Liquid variables as attributes with the `responsive_image_block` tag. This tag works in exactly the same way as the `responsive_image` tag, but is implemented as a block tag to allow for more complex logic.
69
+
70
+
> **Important!** The attributes in the `responsive_image_block` tag are parsed as YAML, so whitespace and indentation are significant!
71
+
72
+
```twig
73
+
{% assign path = 'assets/test.png' %}
74
+
{% assign alt = 'Lorem ipsum...' %}
75
+
76
+
{% responsive_image_block %}
77
+
path: {{ path }}
78
+
alt: {{ alt }}
79
+
{% if title %}
80
+
title: {{ title }}
81
+
{% endif %}
82
+
{% endresponsive_image_block %}
83
+
```
84
+
85
+
## Templates
26
86
27
-
You **must** include a `responsive_image` block in your `_config.yml` for this plugin to work. A full list of the available configuration options is below.
87
+
It's easy to build your own custom templates to render images however you want using the template variables provided by jekyll-responsive-image.
88
+
89
+
### Template Variables
90
+
91
+
The following variables are available in the template:
| `path` | String | The path of the unmodified image. This is always the same as the `path` attribute passed to the tag. |
96
+
| `resized` | Array<Object> | An array of all the resized images. Each image is an **Image Object**. |
97
+
| `original` | Object | An **Image Object** containing information about the original image. |
98
+
| `*` | String | Any other attributes will be passed to the template verbatim as strings (see below). |
99
+
100
+
Any other attributes that are given to the `responsive_image` or `responsive_image_block` tags will be available in the template. For example the following tag will provide an `{{ alt }}` variable to the template:
101
+
102
+
```twig
103
+
{% responsive_image path: assets/my-file.jpg alt: "A description of the image" %}
104
+
```
105
+
106
+
#### Image Objects
107
+
108
+
Image objects (like `original` and each object in `resized`) contain the following properties:
You can use Liquid variables as attributes with the `responsive_image_block` tag. This tag works in exactly the same way as the `responsive_image` tag, but is implemented as a block tag to allow for more complex logic.
120
-
121
-
> **Important!** The attributes in the `responsive_image_block` tag are parsed as YAML, so whitespace and indentation are significant!
122
-
123
-
```twig
124
-
{% assign path = 'assets/test.png' %}
125
-
{% assign alt = 'Lorem ipsum...' %}
126
-
127
-
{% responsive_image_block %}
128
-
path: {{ path }}
129
-
alt: {{ alt }}
130
-
{% if title %}
131
-
title: {{ title }}
132
-
{% endif %}
133
-
{% endresponsive_image_block %}
134
-
```
135
-
136
-
### Template
137
-
138
-
You will need to create a template in order to use the `responsive_image` tag. Below are some sample templates to get you started.
#### Responsive image with `srcset` where the largest resized image is the default
153
-
154
-
> **Note:** This is useful if you don't want your originals to appear on your site. For example, if you're uploading full-res images directly from a device.
155
-
156
-
```twig
157
-
{% capture srcset %}
158
-
{% for i in resized %}
159
-
/{{ i.path }} {{ i.width }}w,
160
-
{% endfor %}
161
-
{% endcapture %}
162
-
163
-
{% assign largest = resized | sort: 'width' | last %}
| `basename` | String | Basename of the file (`assets/some-file.jpg` => `some-file.jpg`). |
222
-
| `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
223
-
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
224
-
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |
225
-
226
-
### Caching
192
+
## Caching
227
193
228
194
You may be able to speed up the build of large sites by enabling render caching. This is usually only effective when the same image is used many times, for example a header image that is rendered in every post.
Render your responsive images using <picture>, with the original asset used as a fallback. Note: If your original assets are not web-friendly (e.g. they are very large), you can use a resized image as the fallback instead. See the srcset-resized-fallback.html template for how to do this.
3
+
4
+
Usage:
5
+
6
+
{% responsive_image path: assets/image.jpg alt: "A description of the image" %}
Render your responsive images using <imgsrcset>, with the original asset used as a fallback. Note: If your original assets are not web-friendly (e.g. they are very large), you might prefer to use the srcset-resized-fallback.html template.
3
+
4
+
Usage:
5
+
6
+
{% responsive_image path: assets/image.jpg alt: "A description of the image" %}
0 commit comments