comparison sat/tools/common/template.py @ 2675:39d187f3698d

template: import contextfilter as module attribute
author Goffi <goffi@goffi.org>
date Tue, 18 Sep 2018 20:55:58 +0200
parents 0fa217fafabf
children da8f3ac86845
comparison
equal deleted inserted replaced
2674:60c9e163f1df 2675:39d187f3698d
57 57
58 from jinja2 import Markup as safe 58 from jinja2 import Markup as safe
59 from jinja2 import is_undefined 59 from jinja2 import is_undefined
60 from jinja2 import utils 60 from jinja2 import utils
61 from jinja2 import TemplateNotFound 61 from jinja2 import TemplateNotFound
62 from jinja2 import contextfilter
62 from jinja2.loaders import split_template_path 63 from jinja2.loaders import split_template_path
63 from lxml import etree 64 from lxml import etree
64 65
65 log = getLogger(__name__) 66 log = getLogger(__name__)
66 67
325 Need to be specified for web rendering, to reflect URL seen by end user 326 Need to be specified for web rendering, to reflect URL seen by end user
326 @param trusted(bool): if True, allow to access absolute path 327 @param trusted(bool): if True, allow to access absolute path
327 Only set to True if environment is safe (e.g. command line tool) 328 Only set to True if environment is safe (e.g. command line tool)
328 @param private(bool): if True, also load sites from sites_path_private_dict 329 @param private(bool): if True, also load sites from sites_path_private_dict
329 """ 330 """
330 # TODO:
331 self.host = host 331 self.host = host
332 self.trusted = trusted 332 self.trusted = trusted
333 self.sites_paths = { 333 self.sites_paths = {
334 u"": os.path.dirname(sat_templates.__file__), 334 u"": os.path.dirname(sat_templates.__file__),
335 } 335 }
580 580
581 return css_files, css_files_noscript 581 return css_files, css_files_noscript
582 582
583 ## custom filters ## 583 ## custom filters ##
584 584
585 @jinja2.contextfilter 585 @contextfilter
586 def _front_url(self, ctx, relative_url): 586 def _front_url(self, ctx, relative_url):
587 """Get front URL (URL seen by end-user) from a relative URL 587 """Get front URL (URL seen by end-user) from a relative URL
588 588
589 This default method return absolute full path 589 This default method return absolute full path
590 """ 590 """
596 596
597 site_root_dir = self.sites_paths[template_data.site] 597 site_root_dir = self.sites_paths[template_data.site]
598 return os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme, 598 return os.path.join(site_root_dir, C.TEMPLATE_TPL_DIR, template_data.theme,
599 relative_url) 599 relative_url)
600 600
601 @jinja2.contextfilter 601 @contextfilter
602 def _next_gidx(self, ctx, value): 602 def _next_gidx(self, ctx, value):
603 """Use next current global index as suffix""" 603 """Use next current global index as suffix"""
604 next_ = ctx["gidx"].next(value) 604 next_ = ctx["gidx"].next(value)
605 return value if next_ == 0 else u"{}_{}".format(value, next_) 605 return value if next_ == 0 else u"{}_{}".format(value, next_)
606 606
607 @jinja2.contextfilter 607 @contextfilter
608 def _cur_gidx(self, ctx, value): 608 def _cur_gidx(self, ctx, value):
609 """Use current current global index as suffix""" 609 """Use current current global index as suffix"""
610 current = ctx["gidx"].current(value) 610 current = ctx["gidx"].current(value)
611 return value if not current else u"{}_{}".format(value, current) 611 return value if not current else u"{}_{}".format(value, current)
612 612
651 _(u'ignoring field "{name}": it doesn\'t exists').format(name=name) 651 _(u'ignoring field "{name}": it doesn\'t exists').format(name=name)
652 ) 652 )
653 continue 653 continue
654 return u" ".join(classes) or None 654 return u" ".join(classes) or None
655 655
656 @jinja2.contextfilter 656 @contextfilter
657 def _item_filter(self, ctx, item, filters): 657 def _item_filter(self, ctx, item, filters):
658 """return item's value, filtered if suitable 658 """return item's value, filtered if suitable
659 659
660 @param item(object): item to filter 660 @param item(object): item to filter
661 value must have name and value attributes, 661 value must have name and value attributes,