Skip to content

Commit 2c5486f

Browse files
authored
Merge pull request #24 from ironland/image-helper
Add readme for image helper
2 parents 1f6cb1f + 978301d commit 2c5486f

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ Example:
389389
{@content data=fields.some_content.value /}
390390
```
391391

392+
### image
393+
394+
The `image` helper returns a markup for `img` tags with proper `srcset` and `sizes` attributes for responsive use. The full readme for the helper can be found [here](docs/helpers/image.md).
395+
396+
392397
### menu
393398

394399
`menu` helper does what it name suggests: it creates a menu. It has several parameters that are explained below:
@@ -655,7 +660,7 @@ public function Query() {
655660
656661
// This returns a WP_Query like object.
657662
// Queried posts are accessible in dust by typing 'Query.posts'.
658-
return \DustPress\Query::get_posts( $args );
663+
return \DustPress\Query::get_posts( $args );
659664
}
660665
```
661666

docs/helpers/image.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### Image
2+
3+
The `image` helper returns a markup for `img` tags with proper `srcset` and `sizes` attributes for responsive use.
4+
5+
You can either give the helper the ID of an image uploaded to WordPress or alternatively use a completely custom URL as the source.
6+
7+
#### An image from WP
8+
9+
When using the helper with an `image ID`, no additional parameters are needed but can be given optionally. The helper fetches the image url from the database and constructs the srcset from the specified WP image sizes.
10+
11+
``` {@image id=image.id /} ```
12+
13+
By default it constructs the same `sizes` attribute for the image as in WP default behaviour. You can also give a custom `sizes` parameter when using the helper with a WP image ID, so that the default WP sizes attribute construction is overriden.
14+
15+
``` {@image id=image.id sizes=Settings.mainImage.sizes /} ```
16+
17+
#### An image from a custom src
18+
19+
When using the helper with a custom src url, you also have to provide the `srcset` and the `sizes` so that the helper is able to produce the responsive HTML markup.
20+
21+
``` {@image src=Settings.mainImage.src srcset=Settings.mainImage.srcset sizes=Settings.mainImage.sizes /} ```
22+
23+
#### The class and alt parameters
24+
25+
With any use case of the helper, you can always optionally provide the `class` and `alt` parameters and they will be added to the returned markup.
26+
27+
If you don't provide the alt parameter when using the helper with an image id, it tries to use the alt attribute given to the image when it was uploaded to WordPress. This attribute might be empty in most cases though, so remember to check the final markup for accessibility purposes.
28+
29+
``` {@image id=image.id class="myclass" alt="An alternative text" /} ```
30+
31+
#### A settings model
32+
33+
We recommend using a separate settings model in your DustPress installation for easily defining custom settings for images. This model can be bound to chosen models or even to the middle model which is extended by page and post models.
34+
35+
The custom image settings for different images in your web site layout can then be easily acquired for use in the templates.
36+
37+
```
38+
<?php
39+
40+
class Settings extends \DustPress\Model {
41+
42+
public function imageSettings() {
43+
return [
44+
'mainImage' => [
45+
'sizes' => [
46+
'(min-width: 1280px) 100vw', '(min-width: 768px) 50vw',
47+
'100vw',
48+
],
49+
'src' => 'https://image.com/example.jpg',
50+
'srcset' => [
51+
'https://image.com/example-300.jpg 300w',
52+
'https://image.com/example-768.jpg 768w',
53+
'https://image.com/example-1024.jpg 1024w',
54+
],
55+
],
56+
];
57+
}
58+
}

0 commit comments

Comments
 (0)