Esempio n. 1
0
 def test_custom_filters(self):
     """Templates: jinja2 custom filters """
     from bottle import jinja2_template as template
     settings = dict(
         filters={"star": lambda var: u"".join((u'*', var, u'*'))})
     t = Jinja2Template("start {{var|star}} end", **settings)
     self.assertEqual("start *var* end", t.render(var="var"))
Esempio n. 2
0
 def test_custom_tests(self):
     """Templates: jinja2 custom tests """
     from bottle import jinja2_template as template
     TEMPL = u"""{% if var is even %}gerade{% else %}ungerade{% endif %}"""
     settings = dict(tests={"even": lambda x: False if x % 2 else True})
     t = Jinja2Template(TEMPL, **settings)
     self.assertEqual("gerade", t.render(var=2))
     self.assertEqual("ungerade", t.render(var=1))
Esempio n. 3
0
def download_pdf():
    env = Environment(loader=PackageLoader(current_app.name, 'templates'))
    template = env.get_template('test.jinja2') # 获得页面模板

    html = template.render(name='大Ren', font_path='/static/simsun.ttf').encode('utf-8')
    result = StringIO()
    pdf = pisa.CreatePDF(StringIO(html), result)
    resp = make_response(result.getvalue())
    resp.headers["Content-Disposition"] = ("attachment; filename='{0}'; filename*=UTF-8''{0}".format('test.pdf'))
    resp.headers['Content-Type'] = 'application/pdf'
    return resp
Esempio n. 4
0
def index():
    distdir = os.path.dirname(os.path.abspath(__file__))
    tpl = os.path.join(distdir, '_templates/index.tpl')
    return Jinja2Template(name=tpl).render(var='var')
Esempio n. 5
0
 def test_string(self):
     """ Templates: Jinja2 string"""
     t = Jinja2Template('start {{var}} end').render(var='var')
     self.assertEqual('start var end', ''.join(t))
Esempio n. 6
0
 def test_inherit(self):
     """ Templates: Jinja2 lookup and inherience """
     t = Jinja2Template(name='jinja2_inherit', lookup=['./views/']).render()
     self.assertEqual('begin abc end', ''.join(t))
Esempio n. 7
0
 def test_name(self):
     """ Templates: Jinja2 lookup by name """
     t = Jinja2Template(name='jinja2_simple',
                        lookup=['./views/']).render(var='var')
     self.assertEqual('start var end', ''.join(t))
Esempio n. 8
0
 def test_file(self):
     """ Templates: Jinja2 file"""
     t = Jinja2Template(name='./views/jinja2_simple.tpl').render(var='var')
     self.assertEqual('start var end', ''.join(t))
Esempio n. 9
0
 def test_file(self):
     """ Templates: Jinja2 file"""
     with chdir(__file__):
         t = Jinja2Template(name='./views/jinja2_simple.tpl',
                            lookup=['.']).render(var='var')
         self.assertEqual('start var end', ''.join(t))