为图像生成<img>标记块。

canonicalwebteam.image-template的Python项目详细描述


为图像生成可执行的HTML图像标记的模块。加价 将:

  • 使用data-src属性 lazysizes
  • 使用预定义的srcset断点
  • 使用?w=XX&h=XX查询调整assets.ubuntu.com图像的大小 参数
  • 为cdn和image的所有图像url加上cloudinary代理url前缀 转换

参数

  • url(强制字符串):图像的url(例如。 https://assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png
  • alt(强制字符串):描述图像的alt文本
  • width(强制整数):图像宽度的像素数 应该是
  • height(强制整数):图像高的像素数 应该是
  • hi_def(强制布尔值):是否已上载图像,宽度和高度是所需大小的2倍
  • extra_classes(可选):要添加到img元素的类字符串
  • 额外属性(可选):额外<img>属性(例如 id)可以作为附加参数传递

用法

image_template函数可以直接用于生成图像 加价。

fromcanonicalwebteamimportimage_templateimage_markup=image_template(url="https://assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png",alt="Operational dashboard",width="1040",height="585",hi_def=True)

但是,最常见的用法是将其添加到django或烧瓶模板中 上下文,作为image函数。

添加懒散尺寸

生成的标记将使用 lazysizes格式-它将 使用data src而不是src并添加lazyload类。

因此,要使用这个插件,您需要在 页。实现这一点的最简单方法是在 <head>

<scriptsrc="https://assets.ubuntu.com/v1/842d27d3-lazysizes.min.js"async=""></script>

django用法

将其添加为模板标记:

# myapp/templatetags.pyfromcanonicalwebteamimportimage_templatefromdjangoimporttemplatefromdjango.utils.safestringimportmark_saferegister=template.Library()@register.simple_tagdefimage(*args,**kwargs):returnmark_safe(image_template(*args,**kwargs))# settings.pyTEMPLATES[0]["OPTIONS"]["builtins"].append("myapp.templatetags")

在模板中使用:

# 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 %}

烧瓶使用

将其添加为模板标记:

# app.pyfromcanonicalwebteamimportimage_templatefromflaskimportFlaskapp=Flask(__name__)@app.context_processordefutility_processor():return{"image":image_template}

在模板中使用:

# 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,
  ) | safe
}}

生成的标记

以上所有示例都将生成以下标记:

<imgdata-srcset="https://res.cloudinary.com/canonical/image/fetch/q_auto,f_auto,fl_sanitize,w_2080,h_1170/https%3A//assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png x2"data-src="https://res.cloudinary.com/canonical/image/fetch/q_auto,f_auto,fl_sanitize,w_1040,h_585/https%3A//assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png"alt="Operational dashboard"width="1040"height="585"class="lazyload"/><noscript><imgsrcset="https://res.cloudinary.com/canonical/image/fetch/q_auto,f_auto,fl_sanitize,w_2080,h_1170/https%3A//assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png x2"src="https://res.cloudinary.com/canonical/image/fetch/q_auto,f_auto,fl_sanitize,w_1040,h_585/https%3A//assets.ubuntu.com/v1/9f6916dd-k8s-prometheus-light.png"alt="Operational dashboard"width="1040"height="585"/></noscript>

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何修复尝试将用户签名到服务器主机时出现的“字符串无法转换为json对象”错误?   控制台Java:不使用新行更新命令行中的文本   java servlet接收xml数据时,出现错误   使用REST API在Bitbucket中复制或复制存储库   java如何在JavaFX中对齐一行?   java如何在活动中显示通过服务获得的数据?   返回BigDecimal作为字符串:返回int   java组织。openqa。硒。InvalidSelectorException:尝试查找元素时选择器无效   java仅在阻塞状态下通知和通知所有影响线程   java JBOSS无法启动部署   java方法的返回值能保证类型安全吗?   JavaeShadoop序列化组织。阿帕奇。hadoop。木卫一。短写失败   java如果我在同一个类上同步了两个方法,它们能同时运行吗?   不使用java数据库的spring分页实现   java如何将字符串切碎成这样的数组?