Generate <img> markup block for an image.
Project description
canonicalwebteam.image-template
A module to generate performant HTML image markup for images. The markup will:
- Use native lazyloading if it's available
- Output markup compatible with lazysizes
noscript
plugin - Explicitly define
width
andheight
attributes to avoid the page jumping effect - Prefix all image URLs with cloudinary CDN proxy URLs, to transform the image to the optimal size
- Use predefined (2x)
srcset
break points for hidef screens
Parameters
url
(mandatory string): The url to an image (e.g.https://assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png
)alt
(mandatory string): Alt text to describe the imagehi_def
(mandatory boolean): Has an image been uploaded 2x the width and height of the desired sizewidth
(mandatory integer): The number of pixels wide the image should beheight
(optional integer): The number of pixels high the image should befill
(optional boolean): Set the crop mode to "fill"loading
(optional string, default: "lazy"): Set to "auto" or "eager" to disable lazyloadingattrs
(optional dictionary): Extra<img>
attributes (e.g.class
orid
) can be passed as additional arguments
Usage
The image_template
function can be used directly to generate image Markup.
from canonicalwebteam import image_template
image_markup = image_template(
url="https://assets.ubuntu.com/v1/450d7c2f-openstack-hero.svg",
alt="",
width="534",
height="319",
hi_def=True,
loading="auto",
fill=True,
attrs={"class": "hero", "id": "openstack-hero"},
)
However, the most common usage is to add it to Django or Flask template contexts, as an image
function.
Add lazysizes
At the time of writing, the loading
attribute is only natively supported in Chrome. Therefore we use lazysizes to enable loading in other browsers while still taking advantage of the native functionality when it's available.
If loading
is set to "lazy" (the default) we will output the Markup in a format supported by lazysizes, with the noscript
and native-loading
plugins enabled.
To support this in your site you need to add the following script to the <head>
of each page, above any <link>
attributes:
<script src="https://assets.ubuntu.com/v1/703e23c9-lazysizes+noscript+native-loading.5.1.2.min.js" defer></script>
Django usage
Add it as a template tag:
# myapp/templatetags.py
from canonicalwebteam import image_template
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag
def image(*args, **kwargs):
return mark_safe(image_template(*args, **kwargs))
# settings.py
TEMPLATES[0]["OPTIONS"]["builtins"].append("myapp.templatetags")
Use it in templates:
# templates/mytemplate.html
{% image url="https://assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png" alt="Operational dashboard" width="1040" height="585" hi_def=True fill=True %}
Flask usage
Add it as a template tag:
# app.py
from canonicalwebteam import image_template
from flask import Flask
app = Flask(__name__)
@app.context_processor
def utility_processor():
return {"image": image_template}
Use it in templates, e.g.::
# templates/mytemplate.html
{{
image(
url="https://assets.ubuntu.com/v1/450d7c2f-openstack-hero.svg",
alt="",
width="534",
height="319",
hi_def=True,
fill=True,
loading="auto",
attrs={"class": "hero", "id": "openstack-hero"},
) | safe
}}
Generated markup
The output image markup will be e.g.:
<img
src="https://res.cloudinary.com/canonical/image/fetch/f_auto,q_auto,fl_sanitize,w_534,h_319,c_fill/https://assets.ubuntu.com/v1/450d7c2f-openstack-hero.svg"
srcset="https://res.cloudinary.com/canonical/image/fetch/f_auto,q_auto,fl_sanitize,w_1068,h_638,c_fill/https://assets.ubuntu.com/v1/450d7c2f-openstack-hero.svg 2x"
alt=""
width="534"
height="319"
loading="auto"
class="hero"
id="openstack hero"
/>
If loading
is set to "lazy" (the default), the output markup will be wrapped in markup for lazysizes support:
<div class="lazyload" data-noscript>
<noscript>
<img ...>
</noscript>
</div>
VS Code Snippet
To add the required markup for this template as a User Snippet, add the following as a HTML snippet (User Snippets under File > Preferences, or Code > Preferences on macOS):
"Image module": {
"prefix": "image-module",
"body": [
"{{",
" image_template(",
" url=\"$1\",",
" alt=\"$2\",",
" height=\"$3\",",
" width=\"$4\",",
" hi_def=$5True,",
" loading=\"auto|lazy$6\",",
" attrs={\"class\": \"$7\"}",
" ) | safe",
"}}"
],
"description": "Image module include"
}"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file canonicalwebteam.image-template-1.3.1.tar.gz
.
File metadata
- Download URL: canonicalwebteam.image-template-1.3.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f1e02cd5c802e80c2dc6273a6749e5e0e7dc83dda5a70f34d7573070100af35 |
|
MD5 | 6efde7adfe1811bc45ef6ba3634ab912 |
|
BLAKE2b-256 | 235e4b048420e01544eecf71d8cc5744a9c05857dcd264cfae17c30c93d962c7 |
File details
Details for the file canonicalwebteam.image_template-1.3.1-py3-none-any.whl
.
File metadata
- Download URL: canonicalwebteam.image_template-1.3.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d803d27a9767b97e8149c3992196ac16ed01181ddaafe4757ebfbd74d21fb7c4 |
|
MD5 | 2ab0f706a937fbc069e4ffa324fda8f0 |
|
BLAKE2b-256 | 6e07354176e3cbeb606f84b2e9360bcead22108c48a021e101040cfa1f543a29 |