python flask cms 模板 标签语义化 如何实现
前言:
程序中定义全局方法
from flask import current_app
@current_app.template_global()
def img_tag(src, alt=None, width=None, height=None):
img_attrs = {}
if alt is not None:
img_attrs['alt'] = alt
if width is not None:
img_attrs['width'] = width
if height is not None:
img_attrs['height'] = height
return f'<img src="{src}" {(" " + " ".join(f"{key}={value}" for key, value in img_attrs.items())) if img_attrs else ""}>'在模板中使用这个全局函数{{ img_tag('path/to/image.jpg', alt='My Image', width=600) }}渲染后的结果
<img src="path/to/image.jpg" alt="My Image" width="600">