本文整理匯總了Python中jinja2.FunctionLoader方法的典型用法代碼示例。如果您正苦於以下問題:Python jinja2.FunctionLoader方法的具體用法?Python jinja2.FunctionLoader怎麽用?Python jinja2.FunctionLoader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jinja2
的用法示例。
在下文中一共展示了jinja2.FunctionLoader方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: render_from_string
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def render_from_string(
self,
input_string=None,
context=None,
template_name=None,
input_dictionary=None
):
if template_name is None:
template_name = 'input.md'
if input_dictionary is None:
input_dictionary = {}
if input_string is not None:
input_dictionary[template_name] = input_string
if context is None:
context = {}
def load_string(template_name):
return input_dictionary[template_name]
loaders = [FunctionLoader(load_string)]
return render_template_to_string(template_name, context, loaders=loaders)
示例2: _create_jinja_environment
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def _create_jinja_environment(runfiles, site_root, link_ext):
def _Load(path):
loc = runfiles.Rlocation(posixpath.join(WORKSPACE_DIR, TEMPLATE_PATH, path))
if loc:
with open(loc, "rb") as f:
return f.read().decode("utf-8")
return None
env = jinja2.Environment(
loader=jinja2.FunctionLoader(_Load),
keep_trailing_newline=True,
line_statement_prefix='%')
env.filters['markdown'] = lambda text: jinja2.Markup(mistune.markdown(text))
env.filters['doc_link'] = (
lambda fname: site_root + '/' + fname + '.' + link_ext)
env.filters['link'] = lambda fname: site_root + '/' + fname
return env
# TODO(dzc): Remove this workaround once we switch to a self-contained Python
# binary format such as PEX.
示例3: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def __init__(self, *args, sql=None, executor=None, **kwargs):
if not executor:
executor = DebugSQLExecutor()
elif isinstance(executor, str):
executor = SQLExecutor.for_kind(executor)()
elif issubclass(executor, SQLExecutor):
executor = executor()
MutableMeasureProvider.__init__(self, *args, **kwargs)
self._base_sql = textwrap.dedent(sql).strip() if sql else None
self.executor = executor
self.dialect = DIALECTS[executor.dialect]
self.add_measure('count', shared=True, distribution='count', default=0)
self._template_environment = jinja2.Environment(loader=jinja2.FunctionLoader(lambda x: x), undefined=jinja2.StrictUndefined)
self._template_environment.filters.update({
'col': self._col,
'val': self._val
})
示例4: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def __init__(self, *args, **kwds):
super(Template, self).__init__(*args, **kwds)
self.jinja = jinja2.Environment(
loader=jinja2.FunctionLoader(self._get_subtemplate), autoescape=True)
示例5: prepare
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
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)
示例6: prepare
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
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)
示例7: prepare
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
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)
示例8: prepare
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def prepare(self, prefix='#', filters=None, tests=None):
from jinja2 import Environment, FunctionLoader
self.env = Environment(line_statement_prefix=prefix,
loader=FunctionLoader(self.loader))
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)
示例9: prepare
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
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)
示例10: _RenderTemplate
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import FunctionLoader [as 別名]
def _RenderTemplate(template_subdir, template_name, **context):
"""Loads a template file and renders it to unicode.
Args:
template_subdir: The subdirectory in gae/templates containing the template
file.
template_name: The name of the template file.
**context: Optional key/value pairs to render into the template.
Returns:
The given template file rendered with the given context as a unicode string.
Raises:
jinja2.TemplateNotFound: if the given template file doesn't exist.
"""
# Create a partial loading function, which will return the contents of the
# template given just the template name.
loading_func = functools.partial(_LoadTemplate, template_subdir)
# Construct an Environment and retrieve the Template.
env = jinja2.Environment(
loader=jinja2.FunctionLoader(loading_func),
autoescape=True,
extensions=['jinja2.ext.autoescape'],
finalize=lambda value: value or '',
variable_start_string='[[',
variable_end_string=']]',
undefined=jinja2.StrictUndefined)
template = env.get_template(template_name)
# Render the template with the provided context.
return template.render(**context)