Add jinja_filters.make_attributes.

Actually it's a function but not a filter. For creating html
attributes out of a dict.
This commit is contained in:
voussoir 2020-01-14 15:10:47 -08:00
parent c185ca6bf7
commit ae6ab72536
2 changed files with 9 additions and 1 deletions

View file

@ -36,8 +36,9 @@ site.jinja_env.lstrip_blocks = True
site.jinja_env.filters['bytestring'] = jinja_filters.bytestring
site.jinja_env.filters['file_link'] = jinja_filters.file_link
site.jinja_env.filters['sort_tags'] = jinja_filters.sort_tags
site.jinja_env.filters['timestamp_to_naturaldate'] = jinja_filters.timestamp_to_naturaldate
site.jinja_env.filters['timestamp_to_8601'] = jinja_filters.timestamp_to_8601
site.jinja_env.filters['timestamp_to_naturaldate'] = jinja_filters.timestamp_to_naturaldate
site.jinja_env.globals['make_attributes'] = jinja_filters.make_attributes
site.debug = True
P = etiquette.photodb.PhotoDB()

View file

@ -18,6 +18,13 @@ def file_link(photo, short=False):
basename = jinja2.filters.do_urlencode(photo.basename)
return f'/file/{photo.id}/{basename}'
def make_attributes(*booleans, **keyvalues):
keyvalues = {key: value for (key, value) in keyvalues.items() if value is not None}
attributes = [f'{key}="{jinja2.filters.escape(value)}"' for (key, value) in keyvalues.items()]
attributes.extend(booleans)
attributes = ' '.join(attributes)
return attributes
def sort_tags(tags):
tags = sorted(tags, key=lambda x: x.name)
return tags