def find_localized_template(name, languages, env=None): """ Get localized template from common name. :param str name: The template name :param list languages: Accepted language strings. :param flask.templating.Environment env: Look up templates using this environment. :raise TemplateNotFound: :return Template: """ # build template preference list candidates = [] for lang in languages: candidates.append(format_template_name(name, lang)) # default template candidates.append(name) # find template for name in candidates: # in template env if env is not None: try: return env.get_template(name) except TemplateNotFound: pass # in inventory if isinstance(TEMPLATES.get(name), Template): return TEMPLATES[name] raise TemplateNotFound( name, message="Cound not find template {!s} (names={!r})".format( name, candidates))
def get_source(self, environment, file_name): source = None template_path = None do_use_cache = self.use_cache.__get__(self, TemplateLoader) theme_folder = settings.get_theme_directory() if theme_folder is None: raise TemplateNotFound(file_name) template_path = str(theme_folder / "templates" / file_name) try: with open(template_path) as f: source = f.read() except FileNotFoundError: pass if source is None: template_folder = settings.get_default_template_directory() template_path = str(template_folder / file_name) with open(template_path) as f: source = f.read() return source, template_path, do_use_cache
def get_source(self, environment, template): if template in self.mapping: source = self.mapping[template] return source, None, lambda: source == self.mapping.get(template) raise TemplateNotFound(template)
def render(template, *args, **kwargs): if template.endswith(".html"): raise TemplateNotFound(template) return real_render(template, *args, **kwargs)
def get_source(self, environment, template_name): template_source = get_template_source(template_name) if template_source: return template_source, template_name, lambda: True else: raise TemplateNotFound(template_name)
def load(self, environment, name, globals=None): loader, local_name = self.get_loader(name) try: return loader.load(environment, local_name, globals) except TemplateNotFound: raise TemplateNotFound(name)
def get_source(self, environment, template): loader, name = self.get_loader(template) try: return loader.get_source(environment, name) except TemplateNotFound: raise TemplateNotFound(template)
'Template corruption in `%s` unicode decode errors' % filename) raise e finally: f.close() mtime = path.getmtime(filename) def uptodate(): try: return path.getmtime(filename) == mtime except OSError: return False return contents, filename, uptodate raise TemplateNotFound(template) class BaseExtension(ext.Extension): ''' Base class for creating custom jinja2 tags. parse expects a tag of the format {% tag_name args, kw %} after parsing it will call _call(args, kw) which must be defined. ''' def parse(self, parser): stream = parser.stream tag = stream.next() # get arguments args = [] kwargs = [] while not stream.current.test_any('block_end'): if args or kwargs:
def get_source(self, environment, template): if not self.has_source_access: raise RuntimeError('%s cannot provide access to the source' % self.__class__.__name__) raise TemplateNotFound(template)
def get_source(self, environment, name): try: return self.scope[name.upper() + 'TEMPLATE'], None, None except KeyError: raise TemplateNotFound(name)
def get_source(self, environment, template): for loader, local_name in self._iter_loaders(template): with suppress(TemplateNotFound, warn=False): return loader.get_source(environment, local_name) raise TemplateNotFound(template)
def update(self, manifest, tag_map, chron_list): raise TemplateNotFound('a template')