Exemple #1
0
 def test_escaping(self):
     self.assertRaises(ParseError, lambda: Template("{{"))
     self.assertRaises(ParseError, lambda: Template("{%"))
     self.assertEqual(Template("{{!").generate(), b("{{"))
     self.assertEqual(Template("{%!").generate(), b("{%"))
     self.assertEqual(
         Template("{{ 'expr' }} {{!jquery expr}}").generate(),
         b("expr {{jquery expr}}"))
Exemple #2
0
 def test_unicode_literal_expression(self):
     # Unicode literals should be usable in templates.  Note that this
     # test simulates unicode characters appearing directly in the
     # template file (with utf8 encoding), i.e. \u escapes would not
     # be used in the template file itself.
     if str is unicode:
         # python 3 needs a different version of this test since
         # 2to3 doesn't run on template internals
         template = Template(utf8(u'{{ "\u00e9" }}'))
     else:
         template = Template(utf8(u'{{ u"\u00e9" }}'))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemple #3
0
 def test_unicode_literal_expression(self):
     # Unicode literals should be usable in templates.  Note that this
     # test simulates unicode characters appearing directly in the
     # template file (with utf8 encoding), i.e. \u escapes would not
     # be used in the template file itself.
     if str is unicode:
         # python 3 needs a different version of this test since
         # 2to3 doesn't run on template internals
         template = Template(utf8(u'{{ "\u00e9" }}'))
     else:
         template = Template(utf8(u'{{ u"\u00e9" }}'))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemple #4
0
    def test_apply(self):
        def upper(s):
            return s.upper()

        template = Template(utf8("{% apply upper %}foo{% end %}"))
        self.assertEqual(template.generate(upper=upper), b("FOO"))
Exemple #5
0
 def test_simple(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name="Ben"), b("Hello Ben!"))
Exemple #6
0
 def test_unicode_template(self):
     template = Template(utf8(u"\u00e9"))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemple #7
0
 def test_expressions(self):
     template = Template("2 + 2 = {{ 2 + 2 }}")
     self.assertEqual(template.generate(), b("2 + 2 = 4"))
Exemple #8
0
 def test_bytes(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name=utf8("Ben")),
                      b("Hello Ben!"))
Exemple #9
0
 def test_comment(self):
     template = Template(utf8("{% comment blah blah %}foo"))
     self.assertEqual(template.generate(), b("foo"))
Exemple #10
0
 def test_if(self):
     template = Template(utf8("{% if x > 4 %}yes{% else %}no{% end %}"))
     self.assertEqual(template.generate(x=5), b("yes"))
     self.assertEqual(template.generate(x=3), b("no"))
Exemple #11
0
 def test_apply(self):
     def upper(s): return s.upper()
     template = Template(utf8("{% apply upper %}foo{% end %}"))
     self.assertEqual(template.generate(upper=upper), b("FOO"))
Exemple #12
0
 def test_unicode_template(self):
     template = Template(utf8(u"\u00e9"))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemple #13
0
 def test_simple(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name="Ben"),
                      b("Hello Ben!"))
Exemple #14
0
 def test_expressions(self):
     template = Template("2 + 2 = {{ 2 + 2 }}")
     self.assertEqual(template.generate(), b("2 + 2 = 4"))
Exemple #15
0
 def test_if(self):
     template = Template(utf8("{% if x > 4 %}yes{% else %}no{% end %}"))
     self.assertEqual(template.generate(x=5), b("yes"))
     self.assertEqual(template.generate(x=3), b("no"))
Exemple #16
0
 def test_bytes(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name=utf8("Ben")), b("Hello Ben!"))
Exemple #17
0
 def test_comment(self):
     template = Template(utf8("{% comment blah blah %}foo"))
     self.assertEqual(template.generate(), b("foo"))
Exemple #18
0
tmpl = Template("""\
<!doctype html>
<html>
  <head>
    <title>{{ page_title }}</title>
  </head>
  <body>
    <div class="header">
      <h1>{{ page_title }}</h1>
    </div>
    <ul class="navigation">
    {% for href, caption in [ \
        ('index.html', 'Index'), \
        ('downloads.html', 'Downloads'), \
        ('products.html', 'Products') \
      ] %}
      <li><a href="{{ href }}">{{ caption }}</a></li>
    {% end %}
    </ul>
    <div class="table">
      <table>
      {% for row in table %}
        <tr>
        {% for cell in row %}
          <td>{{ cell }}</td>
        {% end %}
        </tr>
      {% end %}
      </table>
    </div>
  </body>
</html>\
""")