def test_Template_multi_transform(): class template(wiseguy.template.Template): element = wiseguy.html.jade(''' html head title body div''') transforms = [ wiseguy.template.Transform( "head", lambda head, template: template.element.add( "head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add( "body div", wiseguy.html.Html("Wibble, %s" % body))), wiseguy.template.Transform( ("head", "body"), lambda head, body, template: template.element.add( "body div", wiseguy.html.Html("%s, %s" % (head, body)))) ] template.apply(dict(head="flamble")) assert template.keys() == set(["body"]) html = template.render(body="flimble").strip() expected = """ <html> <head><title>flibble</title></head> <body><div> <p>Wibble, flimble</p> <p>flamble, flimble</p> </div></body> </html>""".strip() assert html == expected
def test_Template_multiple_renderings(): class template(wiseguy.template.Template): element = wiseguy.html.jade(''' html head title body div''') transforms = [ wiseguy.template.Transform( "head", lambda head, template: template.element.add( "head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add( "body div", wiseguy.html.Html("Wibble, %s" % body))), wiseguy.template.Transform( "body", lambda body, template: template.element.add( "body div", wiseguy.html.Html("Wobble, %s" % body))) ] html = template.render(body="Foo").strip() assert template.keys() == set(["body", "head"]) expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected html = template.render(body="Bar").strip() assert template.keys() == set(["body", "head"]) expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Bar</p> <p>Wobble, Bar</p> </div></body> </html>""".strip() assert html == expected
def test_Template_multiple_renderings(): class template(wiseguy.template.Template): element = wiseguy.html.jade( """ html head title body div""" ) transforms = [ wiseguy.template.Transform("head", lambda head, template: template.element.add("head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add("body div", wiseguy.html.Html("Wibble, %s" % body)) ), wiseguy.template.Transform( "body", lambda body, template: template.element.add("body div", wiseguy.html.Html("Wobble, %s" % body)) ), ] html = template.render(body="Foo").strip() assert template.keys() == set(["body", "head"]) expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected html = template.render(body="Bar").strip() assert template.keys() == set(["body", "head"]) expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Bar</p> <p>Wobble, Bar</p> </div></body> </html>""".strip() assert html == expected
def test_Template_multi_transform(): class template(wiseguy.template.Template): element = wiseguy.html.jade( """ html head title body div""" ) transforms = [ wiseguy.template.Transform("head", lambda head, template: template.element.add("head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add("body div", wiseguy.html.Html("Wibble, %s" % body)) ), wiseguy.template.Transform( ("head", "body"), lambda head, body, template: template.element.add( "body div", wiseguy.html.Html("%s, %s" % (head, body)) ), ), ] template.apply(dict(head="flamble")) assert template.keys() == set(["body"]) html = template.render(body="flimble").strip() expected = """ <html> <head><title>flibble</title></head> <body><div> <p>Wibble, flimble</p> <p>flamble, flimble</p> </div></body> </html>""".strip() assert html == expected
def test_Template(): class template(wiseguy.template.Template): element = wiseguy.html.jade(''' html head title body div''') transforms = [ wiseguy.template.Transform( "head", lambda head, template: template.element.add( "head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add( "body div", wiseguy.html.Html("Wibble, %s" % body))), wiseguy.template.Transform( "body", lambda body, template: template.element.add( "body div", wiseguy.html.Html("Wobble, %s" % body))), ] assert template.element assert template.transforms assert len(template.transforms) == 3 assert len(template.applied_transforms) == 0 context = dict(body="Foo") template.apply(context) assert template.keys() == set(["head"]) html = template.element.to_string().strip() expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected assert len(template.transforms) == 1 assert len(template.applied_transforms) == 2 context = dict(body="Bar", head=None) template.apply(context) assert template.keys() == set() html = template.element.to_string().strip() expected = """ <html> <head><title>flibble</title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected assert len(template.transforms) == 0 assert len(template.applied_transforms) == 3
def test_Template(): class template(wiseguy.template.Template): element = wiseguy.html.jade( """ html head title body div""" ) transforms = [ wiseguy.template.Transform("head", lambda head, template: template.element.add("head title", "flibble")), wiseguy.template.Transform( "body", lambda body, template: template.element.add("body div", wiseguy.html.Html("Wibble, %s" % body)) ), wiseguy.template.Transform( "body", lambda body, template: template.element.add("body div", wiseguy.html.Html("Wobble, %s" % body)) ), ] assert template.element assert template.transforms assert len(template.transforms) == 3 assert len(template.applied_transforms) == 0 context = dict(body="Foo") template.apply(context) assert template.keys() == set(["head"]) html = template.element.to_string().strip() expected = """ <html> <head><title></title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected assert len(template.transforms) == 1 assert len(template.applied_transforms) == 2 context = dict(body="Bar", head=None) template.apply(context) assert template.keys() == set() html = template.element.to_string().strip() expected = """ <html> <head><title>flibble</title></head> <body><div> <p>Wibble, Foo</p> <p>Wobble, Foo</p> </div></body> </html>""".strip() assert html == expected assert len(template.transforms) == 0 assert len(template.applied_transforms) == 3