本文整理汇总了Python中jinja.Environment.get_template方法的典型用法代码示例。如果您正苦于以下问题:Python Environment.get_template方法的具体用法?Python Environment.get_template怎么用?Python Environment.get_template使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja.Environment
的用法示例。
在下文中一共展示了Environment.get_template方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dict_loader
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def test_dict_loader():
env = Environment(loader=dict_loader)
tmpl = env.get_template('justdict.html')
assert tmpl.render().strip() == 'FOO'
try:
env.get_template('missing.html')
except TemplateNotFound:
pass
else:
raise AssertionError('expected template exception')
示例2: test_memcached_loader
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def test_memcached_loader():
env = Environment(loader=memcached_loader)
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
tmpl = env.get_template('foo/test.html')
assert tmpl.render().strip() == 'FOO'
try:
env.get_template('missing.html')
except TemplateNotFound:
pass
else:
raise AssertionError('expected template exception')
示例3: test_package_loader
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def test_package_loader():
env = Environment(loader=package_loader)
for x in xrange(2):
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
try:
env.get_template('missing.html')
except TemplateNotFound:
pass
else:
raise AssertionError('expected template exception')
# second run in native mode (no pkg_resources)
package_loader.force_native = True
del package_loader._load_func
示例4: model_run
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def model_run(self, model_context):
context = self._build_context(model_context)
# mostrar el reporte
fileloader = PackageLoader("canasta", "templates")
env = Environment(loader=fileloader)
t = env.get_template("precios.html")
yield PrintHtml(t.render(context))
示例5: BuiltinTemplates
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
class BuiltinTemplates(TemplateBridge):
def init(self, builder):
self.templates = {}
base_templates_path = path.join(path.dirname(__file__), 'templates')
ext_templates_path = [path.join(builder.confdir, dir)
for dir in builder.config.templates_path]
self.templates_path = [base_templates_path] + ext_templates_path
loader = SphinxFileSystemLoader(base_templates_path, ext_templates_path)
if builder.translator is not None:
self.jinja_env = TranslatorEnvironment(loader=loader,
friendly_traceback=False, translator=builder.translator)
else:
self.jinja_env = Environment(loader=loader,
# disable traceback, more likely that something
# in the application is broken than in the templates
friendly_traceback=False)
def newest_template_mtime(self):
return max(mtimes_of_files(self.templates_path, '.html'))
def render(self, template, context):
if template in self.templates:
return self.templates[template].render(context)
templateobj = self.templates[template] = \
self.jinja_env.get_template(template)
return templateobj.render(context)
示例6: render
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def render(printer):
client_address = "<br/>".join(["2 Azalea St.", "Fredericksburg", "22406 VA"])
import datetime
ts = datetime.datetime.today()
datestring = "Date: %s/%s/%s" % (ts.month, ts.day, ts.year)
view_content = view.toHtml()
context = {
"logo": icon,
"company_name": "Conceptive Engineering",
"company_address_1": "L. Van Bauwelstraat 16",
"company_address_2": "2220 Heist-op-den-Berg",
"city": "Belgium",
"date": datestring,
"client_address": client_address,
"client_name": "Client",
"content": view_content,
"signature": "M. Anager",
}
from jinja import Environment, FileSystemLoader
fileloader = FileSystemLoader(settings.CAMELOT_TEMPLATES_DIRECTORY)
e = Environment(loader=fileloader)
t = e.get_template("base.html")
html = t.render(context)
doc = QtGui.QTextDocument()
doc.setHtml(html)
doc.print_(self.printer)
示例7: load_template
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def load_template(config, templateName):
""""Charge un template"""
if config.template_dir == "":
environment = Environment(loader=PackageLoader('tracforge', 'templates'))
else:
environment = Environment(loader=FileSystemLoader(config.template_dir))
return environment.get_template(templateName)
示例8: test_super
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def test_super():
env = Environment(loader=DictLoader({
'a': '{% block intro %}INTRO{% endblock %}|'
'BEFORE|{% block data %}INNER{% endblock %}|AFTER',
'b': '{% extends "a" %}{% block data %}({{ '
'super() }}){% endblock %}',
'c': '{% extends "b" %}{% block intro %}--{{ '
'super() }}--{% endblock %}\n{% block data '
'%}[{{ super() }}]{% endblock %}'
}))
tmpl = env.get_template('c')
assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
示例9: render
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def render(filename, data={}):
import cherrypy
from jinja import Environment, FileSystemLoader
environment = Environment(loader=FileSystemLoader("views"))
data["VERSION"] = encode_unicode(file("VERSION").read()).strip()
try:
data["flash"] = get_flash()
data["flash_class"] = cherrypy.session["flash_class"]
except KeyError:
pass
data.update(helpers)
data["User"] = cherrypy.session.get("User")
return environment.get_template(filename + ".jinja").render(data)
示例10: model_run
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def model_run(self, model_context):
from camelot.view.action_steps import PrintHtml
import datetime
import os
from jinja import Environment, FileSystemLoader
from pkg_resources import resource_filename
fileloader = FileSystemLoader(resource_filename(rms.__name__, 'templates'))
e = Environment(loader=fileloader)
resursa = model_context.get_object()
context = {
'header': resursa.nume,
'title': 'Raport resurse umane',
'style': '.label { font-weight:bold; }',
'persoana': resursa,
'footer': str(datetime.datetime.now().year)
}
t = e.get_template('resurse_umane.html')
yield PrintHtml(t.render(context))
示例11: get
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def get(self):
providers = [{'title': p.title, 'url': p.url, \
'example_url': p.example_url, \
'description': p.__doc__} \
for p in self.providers]
providers.sort(lambda x, y: cmp(x['title'].lower(), y['title'].lower()))
if 'Development' in os.environ['SERVER_SOFTWARE']:
production = False
else:
production = True
env = Environment(loader=FileSystemLoader(os.path.dirname(__file__)))
tmpl = env.get_template('index.jinja')
hostname = os.environ['HTTP_HOST'].lower()
self.response.out.write(tmpl.render(providers=providers,
production=production, hostname=hostname))
示例12: test_choice_loader
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def test_choice_loader():
env = Environment(loader=choice_loader)
tmpl = env.get_template('justdict.html')
assert tmpl.render().strip() == 'FOO'
tmpl = env.get_template('test.html')
assert tmpl.render().strip() == 'BAR'
try:
env.get_template('missing.html')
except TemplateNotFound:
pass
else:
raise AssertionError('expected template exception')
# this should raise an TemplateNotFound error with the
# correct name
try:
env.get_template('brokenimport.html')
except TemplateNotFound, e:
assert e.name == 'missing.html'
示例13: build_examples
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
def build_examples():
funcs = [f for f in listfunc() if f.__name__.startswith('test_')]
examples = []
categories = []
for f in funcs:
fdoc = f.__doc__.split('\n')
cat, title = fdoc[1].strip().split('/')
doc = markdown2.markdown('\n'.join([l.strip() for l in fdoc[2:]]))
name = f.__name__.split('test_')[1]
json = ''
if f():
json = f().render()
code = inspect.getsource(f)
cstart = code.find("'''")
cend = code.find("'''", cstart+3)+3
code = code[:cstart]+ code[cend:]
try:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
lexer = get_lexer_by_name("python", stripall=True)
formatter = HtmlFormatter(linenos=False, cssclass="syntax")
code = highlight(code, lexer, formatter)
except:
pass
if cat not in categories:
categories.append(cat)
example = {
'name': name,
'title': title,
'cat':cat,
'doc': doc,
'code': code,
'datafile': 'data/%s.json' % (name, ),
'json': json
}
examples.append(example)
#generate data files & sidebar links
for e in examples:
outf = open(e['datafile'], 'w')
outf.write(e['json'])
outf.close()
sidebar = '<dl>%s</dl>'
links = []
for cat in categories:
links.append('<dt class="el2">%s</dt><dd>' % cat)
for e in examples: #inefficient.. much
if cat == e['cat']:
links.append('<li><a href="%s">%s</a></li>' %(e['name']+'.html', e['title']))
links.append('</dd>')
sidebar = sidebar % '\n'.join(links)
#generate demo-html files
env = Environment(loader=FileSystemLoader('templates'))
tmpl = env.get_template('code.html')
for e in examples:
fname = e['name']+'.html'
outf = open(fname, 'w')
e.update({'sidebar':sidebar})
output = tmpl.render(**e)
outf.write(output)
outf.close()
#generate doc-html files
env = Environment(loader=FileSystemLoader('templates'))
tmpl = env.get_template('doc.html')
for e in docs:
fname = e['name']+'.html'
outf = open(fname, 'w')
e.update({'sidebar':sidebar})
output = tmpl.render(**e)
outf.write(output)
outf.close()
print 'Build complete.'
print 'Run ./start.sh and visit http://localhost:8000/'
示例14: broken
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
'syntax_broken': '''\
This is an included template
{% raw %}just some foo''',
'multiple_broken': '''\
Just some context:
{% include 'macro_broken' %}
{{ broken() }}
''',
'macro_broken': '''\
{% macro broken %}
{{ 1 / 0 }}
{% endmacro %}
'''
}))
e.globals['fire_multiple_broken'] = lambda: \
e.get_template('multiple_broken').render()
FAILING_STRING_TEMPLATE = '{{ 1 / 0 }}'
BROKEN_STRING_TEMPLATE = '{% if foo %}...{% endfor %}'
def broken():
raise RuntimeError("I'm broken")
def test(environ, start_response):
path = environ.get('PATH_INFO' or '/')
try:
tmpl = e.get_template(path)
except TemplateNotFound:
if path == '/syntax_from_string':
示例15: Environment
# 需要导入模块: from jinja import Environment [as 别名]
# 或者: from jinja.Environment import get_template [as 别名]
# test file for block super support
import jdebug
from jinja import Environment, DictLoader
env = Environment(loader=DictLoader({
'a': '{% block intro %}INTRO{% endblock %}|BEFORE|{% block data %}INNER{% endblock %}|AFTER',
'b': '{% extends "a" %}{% block data %}({{ super() }}){% endblock %}',
'c': '{% extends "b" %}{% block intro %}--{{ super() }}--{% endblock %}\n{% block data %}[{{ super() }}]{% endblock %}'
}))
tmpl = env.get_template('c')
print tmpl.render()