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
- Explicitly define
widthandheightattributes 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)
srcsetbreak 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 lazyloadingfmt(optional string, default: "auto"): Define the file format (e.g.fmt="jpg")attrs(optional dictionary): Extra<img>attributes (e.g.classorid) can be passed as additional argumentsoutput_mode(optional string, default: "html"): The output mode can be set to eitherhtmlorattrs. If set toattrs, the function will return an object with the image attributes instead of HTML markup.
Usage
Local development
For local development, it's best to test this module with one of our website projects like ubuntu.com. For more information, follow this guide (internal only).
Application code
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.
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"
/>
Attribute output
In some cases, you may want to use the image attributes generated by this utility, rather than directly rendering its markup.
This allows you to add the image attributes to an existing <img> tag, or to use them in a different context.
To do this, you can set the output_mode parameter to attrs when calling the image_template function.
For example, some Vanilla patterns may require image attributes to be passed as a dictionary, in order to allow the pattern itself to construct the image element.
{%-
call(slot) vf_tiered_list(
# other params...
img_attrs=image(
url="https://assets.ubuntu.com/v1/3f63a18c-data-center.png",
alt="",
width="2464",
height="1028",
hi_def=True,
loading="auto",
attrs={"class": "my-class-name"},
output_mode="attrs"
)
) -%}
Result:
{
'src': 'https://res.cloudinary.com/canonical/image/fetch/f_auto,q_auto,fl_sanitize,w_2464,h_1028/https://assets.ubuntu.com/v1/3f63a18c-data-center.png',
'srcset': 'https://res.cloudinary.com/canonical/image/fetch/c_limit,f_auto,q_auto,fl_sanitize,w_4928,h_2056/https://assets.ubuntu.com/v1/3f63a18c-data-center.png 2x',
'class': 'my-class-name',
'alt': '',
'width': 2464,
'height': '1028',
'hi_def': True,
'loading': 'auto'
}
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file canonicalwebteam_image_template-1.9.0.tar.gz.
File metadata
- Download URL: canonicalwebteam_image_template-1.9.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1034a97f065a96ae6c80a07c57eb303e18404fd9b392f61f0731653b997d2a36
|
|
| MD5 |
b6a1471cabe4bc8d634ed026e8909819
|
|
| BLAKE2b-256 |
ff3b73850961542c081498b9dc506f0fbb61124a628f353f29192d4228784ffa
|
File details
Details for the file canonicalwebteam_image_template-1.9.0-py3-none-any.whl.
File metadata
- Download URL: canonicalwebteam_image_template-1.9.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc122a07cf835ab57385cb0f82d0977d32d10fea802b94e5dd1d60099a577ee4
|
|
| MD5 |
45bd7917fd6d3c8fe29e0943702c7835
|
|
| BLAKE2b-256 |
72729bdcbc83dc43df5bf86c8c53619b294cc13834954d6aed0af68bbc789f36
|