Skip to content

Commit 3cf3bc7

Browse files
Move sample templates into sample-templates/ directory; update README
1 parent d902ec7 commit 3cf3bc7

File tree

5 files changed

+177
-135
lines changed

5 files changed

+177
-135
lines changed

README.md

+101-135
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# jekyll-responsive-image
22

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/).
44

55
[![Build Status](https://img.shields.io/travis/wildlyinaccurate/jekyll-responsive-image.svg?style=flat-square)](https://travis-ci.org/wildlyinaccurate/jekyll-responsive-image)
66
[![Coverage Status](https://img.shields.io/coveralls/wildlyinaccurate/jekyll-responsive-image.svg?style=flat-square)](https://coveralls.io/repos/github/wildlyinaccurate/jekyll-responsive-image/badge.svg?branch=master)
77

88
## Installation
99

10-
First, install the gem:
10+
This plugin can be installed in three steps:
11+
12+
### 1. Install the gem
13+
14+
Either add `jekyll-responsive-image` to your Gemfile, or run the following command to install the gem:
1115

1216
```
1317
$ gem install jekyll-responsive-image
1418
```
1519

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`:
1721

1822
```yaml
1923
gems:
@@ -22,9 +26,100 @@ gems:
2226
2327
Or you can copy the contents of [`responsive_image.rb`](lib/jekyll-responsive-image.rb) into your `_plugins` directory.
2428

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.
55+
56+
```twig
57+
{% responsive_image path: assets/my-file.jpg template: _includes/another-template.html %}
58+
```
59+
60+
Any extra attributes will be passed straight to the template as variables.
61+
62+
```twig
63+
{% responsive_image path: assets/image.jpg alt: "Lorem ipsum..." title: "Lorem ipsum..." %}
64+
```
65+
66+
### Liquid variables as attributes
67+
68+
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
2686

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:
92+
93+
| Variable | Type | Description |
94+
|------------|---------------|------------------------------------------------------------------------------------------------------|
95+
| `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:
109+
110+
| Variable | Type | Description |
111+
|-------------|---------|----------------------------------------------------------------------------------------------|
112+
| `path` | String | The path to the image. |
113+
| `width` | Integer | The width of the image. |
114+
| `height` | Integer | The height of the image. |
115+
| `basename` | String | Basename of the file (`assets/some-file.jpg` => `some-file.jpg`). |
116+
| `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
117+
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
118+
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |
119+
120+
## All configuration options
121+
122+
A full list of all of the available configuration options is below.
28123

29124
```yaml
30125
responsive_image:
@@ -94,136 +189,7 @@ responsive_image:
94189
- assets/avatars/*.{jpeg,jpg}
95190
```
96191
97-
## Usage
98-
99-
Replace your images with the `responsive_image` tag, specifying the path to the image in the `path` attribute.
100-
101-
```twig
102-
{% responsive_image path: assets/my-file.jpg %}
103-
```
104-
105-
You can override the template on a per-image basis by specifying the `template` attribute.
106-
107-
```twig
108-
{% responsive_image path: assets/my-file.jpg template: _includes/another-template.html %}
109-
```
110-
111-
Any extra attributes will be passed straight to the template as variables.
112-
113-
```twig
114-
{% responsive_image path: assets/image.jpg alt: "Lorem ipsum..." title: "Lorem ipsum..." %}
115-
```
116-
117-
### Liquid variables as attributes
118-
119-
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.
139-
140-
#### Responsive images with `srcset`
141-
142-
```twig
143-
{% capture srcset %}
144-
{% for i in resized %}
145-
/{{ i.path }} {{ i.width }}w,
146-
{% endfor %}
147-
{% endcapture %}
148-
149-
<img src="/{{ path }}" alt="{{ alt }}" srcset="{{ srcset | strip_newlines }}">
150-
```
151-
152-
#### 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 %}
164-
165-
<img src="/{{ largest.path }}" alt="{{ alt }}" srcset="{{ srcset | strip_newlines }}">
166-
```
167-
168-
#### Responsive images with `<picture>`
169-
170-
```twig
171-
<picture>
172-
{% for i in resized %}
173-
<source media="(min-width: {{ i.width }}px)" srcset="/{{ i.path }}">
174-
{% endfor %}
175-
<img src="/{{ path }}">
176-
</picture>
177-
```
178-
179-
#### Responsive images using [Imager.js](https://github.com/BBC-News/Imager.js/)
180-
181-
> **Note:** This template assumes an `output_path_format` of `assets/resized/%{width}/%{basename}`
182-
183-
```twig
184-
{% assign smallest = resized | sort: 'width' | first %}
185-
186-
<div class="responsive-image">
187-
<img class="responsive-image__placeholder" src="/{{ smallest.path }}">
188-
<div class="responsive-image__delayed" data-src="/assets/resized/{width}/{{ original.basename }}"></div>
189-
</div>
190-
191-
<script>
192-
new Imager('.responsive-image__delayed', {
193-
availableWidths: [{{ resized | map: 'width' | join: ', ' }}]
194-
onImagesReplaced: function() {
195-
$('.responsive-image__placeholder').hide();
196-
}
197-
});
198-
</script>
199-
```
200-
201-
### Template Variables
202-
203-
The following variables are available in the template:
204-
205-
| Variable | Type | Description |
206-
|----------- |--------|------------------------------------------------------------------------------------------------------|
207-
| `path` | String | The path of the unmodified image. This is always the same as the `path` attribute passed to the tag. |
208-
| `resized` | Array | An array of all the resized images. Each image is an **Image Object**. |
209-
| `original` | Object | An **Image Object** containing information about the original image. |
210-
| `*` | String | Any other attributes will be passed to the template verbatim as strings. |
211-
212-
#### Image Objects
213-
214-
Image objects (like `original` and each object in `resized`) contain the following properties:
215-
216-
| Variable | Type | Description |
217-
|-------------|---------|----------------------------------------------------------------------------------------------|
218-
| `path` | String | The path to the image. |
219-
| `width` | Integer | The width of the image. |
220-
| `height` | Integer | The height of the image. |
221-
| `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
227193
228194
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.
229195

sample-templates/imager-js.html

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% comment %}
2+
Render your responsive images using Imager.js (https://github.com/BBC-News/Imager.js/), with the smallest resized image used as a fallback.
3+
4+
Usage:
5+
6+
{% responsive_image path: assets/image.jpg alt: "A description of the image" %}
7+
8+
(P.S. You can safely delete this comment block)
9+
{% endcomment %}
10+
11+
{% assign smallest = resized | sort: 'width' | first %}
12+
13+
<div class="responsive-image">
14+
<img class="responsive-image__placeholder" src="/{{ smallest.path }}">
15+
<div class="responsive-image__delayed" data-src="/assets/resized/{width}/{{ original.basename }}"></div>
16+
</div>
17+
18+
<script>
19+
new Imager('.responsive-image__delayed', {
20+
availableWidths: [{{ resized | map: 'width' | join: ', ' }}]
21+
onImagesReplaced: function() {
22+
$('.responsive-image__placeholder').hide();
23+
}
24+
});
25+
</script>

sample-templates/picture.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% comment %}
2+
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" %}
7+
8+
(P.S. You can safely delete this comment block)
9+
{% endcomment %}
10+
11+
<picture>
12+
{% for i in resized %}
13+
<source media="(min-width: {{ i.width }}px)" srcset="/{{ i.path }}">
14+
{% endfor %}
15+
<img src="/{{ path }}">
16+
</picture>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% comment %}
2+
Render your responsive images using <img srcset>, with the largest resized image used as a fallback.
3+
4+
Usage:
5+
6+
{% responsive_image path: assets/image.jpg alt: "A description of the image" %}
7+
8+
(P.S. You can safely delete this comment block)
9+
{% endcomment %}
10+
11+
{% assign largest = resized | sort: 'width' | last %}
12+
{% capture srcset %}
13+
{% for i in resized %}
14+
/{{ i.path }} {{ i.width }}w,
15+
{% endfor %}
16+
{% endcapture %}
17+
18+
<img src="/{{ largest.path }}" alt="{{ alt }}" srcset="{{ srcset | strip_newlines }}">

sample-templates/srcset.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% comment %}
2+
Render your responsive images using <img srcset>, 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" %}
7+
8+
(P.S. You can safely delete this comment block)
9+
{% endcomment %}
10+
11+
{% capture srcset %}
12+
{% for i in resized %}
13+
/{{ i.path }} {{ i.width }}w,
14+
{% endfor %}
15+
{% endcapture %}
16+
17+
<img src="/{{ path }}" alt="{{ alt }}" srcset="{{ srcset | strip_newlines }}">

0 commit comments

Comments
 (0)