我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用jinja2.FunctionLoader()。
def setup_pp_env(page=None, page_filters=[], no_loader=False): remote, path = get_page_where(page) if remote: logger.debug("Using remote template loader for page %s" % page) pp_env = jinja2.Environment(loader=jinja2.FunctionLoader(read_markdown_remote)) elif no_loader: logger.debug("Using a no-loader Jinja environment") pp_env = jinja2.Environment() else: logger.debug("Using FileSystemLoader for page %s" % page) pp_env = jinja2.Environment(loader=jinja2.FileSystemLoader(path)) # Pull exported values (& functions) from page filters into the pp_env for filter_name in page_filters: if filter_name not in config.filters.keys(): logger.debug("Skipping unloaded filter '%s'" % filter_name) continue if "export" in dir(config.filters[filter_name]): for key,val in config.filters[filter_name].export.items(): logger.debug("... pulling in filter_%s's exported key '%s'" % (filter_name, key)) pp_env.globals[key] = val return pp_env
def prepare(self, filters=None, tests=None, globals={}, **kwargs): from jinja2 import Environment, FunctionLoader self.env = Environment(loader=FunctionLoader(self.loader), **kwargs) if filters: self.env.filters.update(filters) if tests: self.env.tests.update(tests) if globals: self.env.globals.update(globals) if self.source: self.tpl = self.env.from_string(self.source) else: self.tpl = self.env.get_template(self.name)
def prepare(self, filters=None, tests=None, globals={}, **kwargs): from jinja2 import Environment, FunctionLoader if 'prefix' in kwargs: # TODO: to be removed after a while raise RuntimeError('The keyword argument `prefix` has been removed. ' 'Use the full jinja2 environment name line_statement_prefix instead.') self.env = Environment(loader=FunctionLoader(self.loader), **kwargs) if filters: self.env.filters.update(filters) if tests: self.env.tests.update(tests) if globals: self.env.globals.update(globals) if self.source: self.tpl = self.env.from_string(self.source) else: self.tpl = self.env.get_template(self.filename)
def prepare(self, filters=None, tests=None, globals={}, **kwargs): from jinja2 import Environment, FunctionLoader self.env = Environment(loader=FunctionLoader(self.loader), **kwargs) if filters: self.env.filters.update(filters) if tests: self.env.tests.update(tests) if globals: self.env.globals.update(globals) if self.source: self.tpl = self.env.from_string(self.source) else: self.tpl = self.env.get_template(self.filename)
def render_jinja(dict_, template_str): """Render dict onto jinja template and return the string result""" name = 'jvars' def loader(n): if n == name: return template_str else: kkdir = pathfinder.PathFinder.find_kolla_kubernetes_dir() path = os.path.join(kkdir, n) with open(path) as f: d = f.read() return d j2env = jinja2.Environment( loader=jinja2.FunctionLoader(loader)) # Do not print type for bools "!!bool" on output j2env.filters['bool'] = TypeUtils.str_to_bool j2env.filters['min'] = min j2env.filters['b64encode'] = lambda x: \ base64.b64encode(x.encode('ascii')) j2env.filters['b64decode'] = base64.b64decode j2env.filters['to_nice_yaml'] = lambda x: \ yaml.safe_dump(x, default_flow_style=False) if x else '' # Add a "raise" keyword for raising exceptions from within jinja def jinja_raise(message): raise Exception(message) j2env.globals['raise'] = jinja_raise # Add a keyword for accessing KubeUtils from within jinja j2env.globals['KubeUtils'] = KubeUtils # Render the template rendered_template = j2env.get_template(name).render(dict_) return rendered_template + "\n"
def prepare(self, filters=None, tests=None, **kwargs): from jinja2 import Environment, FunctionLoader if 'prefix' in kwargs: # TODO: to be removed after a while raise RuntimeError('The keyword argument `prefix` has been removed. ' 'Use the full jinja2 environment name line_statement_prefix instead.') self.env = Environment(loader=FunctionLoader(self.loader), **kwargs) if filters: self.env.filters.update(filters) if tests: self.env.tests.update(tests) if self.source: self.tpl = self.env.from_string(self.source) else: self.tpl = self.env.get_template(self.filename)
def _make_environment(load, additionals, extensions): extensions.append(ext.with_) env = jinja2.Environment( loader=jinja2.FunctionLoader(load), undefined=jinja2.StrictUndefined, trim_blocks=True, lstrip_blocks=True, extensions=extensions, ) for name, defs in additionals.items(): getattr(env, name).update(defs) return env