changeset 2385:39d30cf722cb

template: gidx methods improvment: gidx methods now use the filtered name to return global indexes. For instance, if "widget" is filtered name, first index will be "widget", then "widget_1", "widget_2" and so one. If an other name is used, it restart (e.g. for "label": "label", "label_1", "label_2", etc.).
author Goffi <goffi@goffi.org>
date Mon, 16 Oct 2017 07:48:09 +0200
parents d14c1a3e3244
children 2e05921df16a
files src/tools/common/template.py
diffstat 1 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/common/template.py	Mon Oct 16 07:44:08 2017 +0200
+++ b/src/tools/common/template.py	Mon Oct 16 07:48:09 2017 +0200
@@ -126,14 +126,17 @@
     """Index global to a page"""
 
     def __init__(self):
-        self._idx = 0
+        self._indexes = {}
 
-    def next(self):
-        self._idx+=1
-        return self._idx
+    def next(self, value):
+        if value not in self._indexes:
+            self._indexes[value] = 0
+            return 0
+        self._indexes[value] += 1
+        return self._indexes[value]
 
-    def current(self):
-        return self._idx
+    def current(self, value):
+        return self._indexes.get(value)
 
 
 class ScriptsHandler(object):
@@ -319,12 +322,14 @@
     @jinja2.contextfilter
     def _next_gidx(self, ctx, value):
         """Use next current global index as suffix"""
-        return u"{}_{}".format(value, ctx['gidx'].next())
+        next_ = ctx['gidx'].next(value)
+        return value if next_ == 0 else u"{}_{}".format(value, next_)
 
     @jinja2.contextfilter
     def _cur_gidx(self, ctx, value):
         """Use current current global index as suffix"""
-        return u"{}_{}".format(value, ctx['gidx'].current())
+        current = ctx['gidx'].current(value)
+        return value if not current else u"{}_{}".format(value, current)
 
     def _date_days(self, timestamp):
         return int(time.time() - int(timestamp))/(3600*24)