Mercurial > libervia-backend
changeset 4354:4a8bb49b9d0f
template: Add `initials` filter.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 11 Apr 2025 18:19:17 +0200 |
parents | e22a5d80be02 |
children | 01ee3b902d33 |
files | libervia/backend/tools/common/template.py |
diffstat | 1 files changed, 12 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/libervia/backend/tools/common/template.py Fri Apr 11 18:18:50 2025 +0200 +++ b/libervia/backend/tools/common/template.py Fri Apr 11 18:19:17 2025 +0200 @@ -497,6 +497,7 @@ self.env.globals["C"] = C # custom filters + self.env.filters["initials"] = self._initials self.env.filters["bare_jid"] = self._bare_jid self.env.filters["next_gidx"] = self._next_gidx self.env.filters["cur_gidx"] = self._cur_gidx @@ -799,6 +800,17 @@ """Return the bare JID""" return str(jid.JID(str(full_jid)).bare) + def _initials(self, name: str) -> str: + """Return the uppercased initials of a name.""" + name_parts = name.strip().split() + if not name_parts: + initials = "??" + elif len(name_parts) == 1: + initials = name_parts[0][:2] + else: + initials = name_parts[0][0] + name_parts[1][0] + return initials.upper() + @pass_context def _next_gidx(self, ctx, value): """Use next current global index as suffix"""