def render_jinja2_file(name, context, jinja2_filters=default_jinja2_filters_path): """Render jinja2 file name with context""" path, filename = os.path.split(name) env = jinja2.Environment( undefined=jinja2.StrictUndefined, loader=jinja2.FileSystemLoader(path or './'), trim_blocks=True, lstrip_blocks=True, extensions=['jinja2.ext.do'], ) load_jinja2_filters(env) load_jinja2_filters_from_file(env, jinja2_filters) return env.get_template(filename).render(context)
def render_jinja2_file(name, context, jinja2_filters=defaults.DEFAULT_JINJA2_FILTERS_PATH, search_paths=None): """Render jinja2 file name with context""" path, filename = os.path.split(name) search_paths = [path or "./"] + (search_paths or []) env = jinja2.Environment( undefined=jinja2.StrictUndefined, loader=jinja2.FileSystemLoader(search_paths), trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.do"], ) load_jinja2_filters(env) load_jinja2_filters_from_file(env, jinja2_filters) return env.get_template(filename).render(context)
def render_jinja2_file(name, context, jinja2_filters=defaults.DEFAULT_JINJA2_FILTERS_PATH, search_paths=None): """Render jinja2 file name with context""" path, filename = os.path.split(name) search_paths = [path or "./"] + (search_paths or []) env = jinja2.Environment( undefined=jinja2.StrictUndefined, loader=jinja2.FileSystemLoader(search_paths), trim_blocks=True, lstrip_blocks=True, extensions=["jinja2.ext.do"], ) load_jinja2_filters(env) load_jinja2_filters_from_file(env, jinja2_filters) try: return env.get_template(filename).render(context) except jinja2.TemplateError as e: # Exception misses the line number info. Retreive it from traceback err_info = _jinja_error_info(traceback.extract_tb(sys.exc_info()[2])) raise CompileError( f"Jinja2 TemplateError: {e}, at {err_info[0]}:{err_info[1]}")