From d047be7ba9fd0921c7c32d4e7bcdb3a259705a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 8 Jul 2020 00:58:25 +0800 Subject: [PATCH] Added the format template tag that does sprintf-like string format in the templates. --- accounting/templates/accounting/cash.html | 3 ++- mia_core/templatetags.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/accounting/templates/accounting/cash.html b/accounting/templates/accounting/cash.html index eb6c620..69e45e0 100644 --- a/accounting/templates/accounting/cash.html +++ b/accounting/templates/accounting/cash.html @@ -24,7 +24,8 @@ First written: 2020/7/1 {% load humanize %} {% block settings %} - {% trans "Cash Report" context "Accounting|" as title %} + {% trans "Cash Account for {} in {}" context "Accounting|" as title_format %} + {% format title_format "庫存現金" period_parser.description as title %} {% setvar "title" title %} {% endblock %} diff --git a/mia_core/templatetags.py b/mia_core/templatetags.py index 1580090..b357354 100644 --- a/mia_core/templatetags.py +++ b/mia_core/templatetags.py @@ -30,3 +30,19 @@ def setvar(context, key, value): """ context.dicts[0][key] = value return "" + + +@register.simple_tag(takes_context=True) +def format(context, format, *args, **kwargs): + """Sets a variable in the template. + + Args: + context (Context): the context. + format (str): The format. + args (str): The parameters. + kwargs (str): The keyword arguments. + """ + if "as" in kwargs: + context.dicts[0][kwargs["as"]] = format.format(*args) + return "" + return format.format(*args)