Skip to main content

Generate <img> markup block for an image.

Project description


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 and height 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


  • url (mandatory string): The url to an image (e.g.
  • alt (mandatory string): Alt text to describe the image
  • width (mandatory integer): The number of pixels wide the image should be
  • height (mandatory integer): The number of pixels high the image should be
  • hi_def (mandatory boolean): Has an image been uploaded 2x the width and height of the desired size
  • loading (optional string, default: "lazy"): Set to "auto" or "eager" to disable lazyloading
  • attrs (optional dictionary): Extra <img> attributes (e.g. class or id) can be passed as additional arguments


The image_template function can be used directly to generate image Markup.

from canonicalwebteam import image_template

image_markup = image_template(
    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="" defer></script>

Django usage

Add it as a template tag:

# myapp/

from canonicalwebteam import image_template
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

def image(*args, **kwargs):
    return mark_safe(image_template(*args, **kwargs))



Use it in templates:

# templates/mytemplate.html

{% image url="" alt="Operational dashboard" width="1040" height="585" hi_def=True %}

Flask usage

Add it as a template tag:


from canonicalwebteam import image_template
from flask import Flask

app = Flask(__name__)

def utility_processor():
    return {"image": image_template}

Use it in templates, e.g.::

# templates/mytemplate.html

    attrs={"class": "hero", "id": "openstack-hero"},
  ) | safe

Generated markup

The output image markup will be e.g.:

    srcset=",q_auto,fl_sanitize,w_1068,h_638/ 2x"
    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>
    <img ...>

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for canonicalwebteam.image-template, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size canonicalwebteam.image-template-1.0.0.tar.gz (4.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page