From 91a17533f0fc7dacaf826fe52430690ebc476fbb Mon Sep 17 00:00:00 2001 From: Peter Verhas Date: Fri, 2 Mar 2018 16:41:06 +0100 Subject: [PATCH 1/2] prefix should be terminated with ':' we do not want to process `manager:ifjustanygarbage can come here: /Delete` --- engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine.py b/engine.py index b13899d..94a20a1 100644 --- a/engine.py +++ b/engine.py @@ -5,14 +5,14 @@ class SuperFormatter(string.Formatter): """World's simplest Template engine.""" def format_field(self, value, spec): - if spec.startswith('repeat'): + if spec.startswith('repeat:'): template = spec.partition(':')[-1] if type(value) is dict: value = value.items() return ''.join([template.format(item=item) for item in value]) elif spec == 'call': return value() - elif spec.startswith('if'): + elif spec.startswith('if:'): return (value and spec.partition(':')[-1]) or '' else: return super(SuperFormatter, self).format_field(value, spec) From 2e36bdf2b23d79559b07cc8b02fdeb696794ac7c Mon Sep 17 00:00:00 2001 From: Peter Verhas Date: Tue, 6 Mar 2018 16:04:07 +0100 Subject: [PATCH 2/2] recursive call is corrected The call in the `repeat` was invoking the `string` formatter. The recursive call will allow loops in loops, call and other spec formatting in loops. --- engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine.py b/engine.py index 94a20a1..4c949fe 100644 --- a/engine.py +++ b/engine.py @@ -9,7 +9,7 @@ def format_field(self, value, spec): template = spec.partition(':')[-1] if type(value) is dict: value = value.items() - return ''.join([template.format(item=item) for item in value]) + return ''.join([self.format(template,item=item) for item in value]) elif spec == 'call': return value() elif spec.startswith('if:'):