diff options
author | Håkon Vågsether <hauk142@gmail.com> | 2021-08-21 12:12:47 +0200 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2021-09-07 07:13:44 -0400 |
commit | 774d30d66afdab0c16d9239d84e5829187f791c4 (patch) | |
tree | 2e3c4ca9b21bd3fa9ab426fe50bae23406a0192f | |
parent | 956f332e410331fde426e1485589d905cdf80fb1 (diff) |
grc: Add no_quotes() convenience function (callable from templates)
Signed-off-by: Håkon Vågsether <hauk142@gmail.com>
-rw-r--r-- | grc/core/blocks/_templates.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/grc/core/blocks/_templates.py b/grc/core/blocks/_templates.py index c98e277feb..37775cea1d 100644 --- a/grc/core/blocks/_templates.py +++ b/grc/core/blocks/_templates.py @@ -14,6 +14,16 @@ from mako.exceptions import SyntaxException from ..errors import TemplateError +# The utils dict contains convenience functions +# that can be called from any template +def no_quotes(string, fallback=None): + if len(string) > 2: + if str(string)[0] + str(string)[-1] in ("''", '""'): + return str(string)[1:-1] + return str(fallback if fallback else string) + +utils = {'no_quotes': no_quotes} + class MakoTemplates(dict): @@ -53,6 +63,7 @@ class MakoTemplates(dict): if not text: return '' namespace = self.instance.namespace_templates + namespace = {**namespace, **utils} try: if isinstance(text, list): |