Mercurial > libervia-backend
comparison src/tools/common/template.py @ 2384:d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 16 Oct 2017 07:44:08 +0200 |
parents | 2dae79990122 |
children | 39d30cf722cb |
comparison
equal
deleted
inserted
replaced
2383:a37457da2bb7 | 2384:d14c1a3e3244 |
---|---|
194 self.env.globals[u'C'] = C | 194 self.env.globals[u'C'] = C |
195 # custom filters | 195 # custom filters |
196 self.env.filters['next_gidx'] = self._next_gidx | 196 self.env.filters['next_gidx'] = self._next_gidx |
197 self.env.filters['cur_gidx'] = self._cur_gidx | 197 self.env.filters['cur_gidx'] = self._cur_gidx |
198 self.env.filters['date_days'] = self._date_days | 198 self.env.filters['date_days'] = self._date_days |
199 self.env.filters['xmlui_class'] = self._xmlui_class | |
199 | 200 |
200 def installTranslations(self): | 201 def installTranslations(self): |
201 i18n_dir = os.path.join(self.base_dir, 'i18n') | 202 i18n_dir = os.path.join(self.base_dir, 'i18n') |
202 self.translations = {} | 203 self.translations = {} |
203 for lang_dir in os.listdir(i18n_dir): | 204 for lang_dir in os.listdir(i18n_dir): |
326 return u"{}_{}".format(value, ctx['gidx'].current()) | 327 return u"{}_{}".format(value, ctx['gidx'].current()) |
327 | 328 |
328 def _date_days(self, timestamp): | 329 def _date_days(self, timestamp): |
329 return int(time.time() - int(timestamp))/(3600*24) | 330 return int(time.time() - int(timestamp))/(3600*24) |
330 | 331 |
332 def attr_escape(self, text): | |
333 """escape a text to a value usable as an attribute | |
334 | |
335 remove spaces, and put in lower case | |
336 """ | |
337 return text.strip().lower().replace(' ', '_') | |
338 | |
339 def _xmlui_class(self, xmlui_item, fields): | |
340 """return classes computed from XMLUI fields name | |
341 | |
342 will return a string with a series of escaped {name}_{value} separated by spaces. | |
343 @param xmlui_item(xmlui.XMLUIPanel): XMLUI containing the widgets to use | |
344 @param fields(iterable(unicode)): names of the widgets to use | |
345 @return (unicode, None): computer string to use as class attribute value | |
346 None if no field was specified | |
347 """ | |
348 classes = [] | |
349 for name in fields: | |
350 escaped_name = self.attr_escape(name) | |
351 try: | |
352 for value in xmlui_item.widgets[name].values: | |
353 classes.append(escaped_name + '_' + self.attr_escape(value)) | |
354 except KeyError: | |
355 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) | |
356 continue | |
357 return u' '.join(classes) or None | |
358 | |
331 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): | 359 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): |
332 """render a template | 360 """render a template |
333 | 361 |
334 @param template(unicode): template to render (e.g. blog/articles.html) | 362 @param template(unicode): template to render (e.g. blog/articles.html) |
335 @param theme(unicode): template theme | 363 @param theme(unicode): template theme |