def test_page(self): self.maxDiff = None expected = '<!DOCTYPE HTML><html><head><meta charset="utf-8"/><link href="my.css" type="text/css" ' \ 'rel="stylesheet"/><title>test_title</title></head><body><div class="linkBox"><a href="www.' \ 'foo.com">www.foo.com</a></div><p>This is foo</p><p>This is Bar</p><p>Have you met my friend Baz?' \ '</p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</body></html>' my_text_list = [ 'This is foo', 'This is Bar', 'Have you met my friend Baz?' ] another_list = [ 'Lorem ipsum ', 'dolor sit amet, ', 'consectetur adipiscing elit' ] page = Html( )( # add tags inside the one you created calling the parent Head()( # add multiple tags in one call Meta( charset='utf-8' ), # add tag attributes using kwargs in tag initialization Link(href="my.css", typ="text/css", rel="stylesheet"), Title('test_title')), body=Body() ( # give them a name so you can navigate the DOM with those names Div(klass='linkBox')(A(href='www.foo.com')), (P()(text) for text in my_text_list), # tag insertion accepts generators another_list # add text from a list, str.join is used in rendering )) self.assertEqual(Counter(page.render()), Counter(expected))
def test_create_call_singletag(self): head = Head() self.page(head) self.is_tag(head) self.assertEqual(len(self.page.childs), 1) self.assertEqual(self.page.length, 1) self.assertEqual(self.page.childs[0], head) self.assertEqual(self.page.first(), head) self.assertEqual(self.page.last(), head)
def test_create_call_generator(self): g = (t for t in [Head(), Body()]) self.page(g) head, body = self.page.childs self.check_head_body(head, body)
def test_create_call_tuple(self): t = (Head(), Body()) self.page(t) self.check_head_body(*t)
def test_create_call_list(self): l = [Head(), Body()] self.page(l) self.check_head_body(*l)
def test_create_call_multitag(self): head = Head() body = Body() self.page(head, body) self.check_head_body(head, body)
from tempy.tags import Html, Head, Body, Meta, Link, Div, P, A my_text_list = ['This is foo', 'This is Bar', 'Have you met my friend Baz?'] another_list = [ 'Lorem ipsum ', 'dolor sit amet, ', 'consectetur adipiscing elit' ] # make tags instantiating TemPy objects page = Html()( # add tags inside the one you created calling the parent Head()( # add multiple tags in one call Meta(charset='utf-8' ), # add tag attributes using kwargs in tag initialization Link(href="my.css", typ="text/css", rel="stylesheet")), body=Body() ( # give them a name so you can navigate the DOM with those names Div(klass='linkBox')(A(href='www.foo.com')), (P()(text) for text in my_text_list), # tag insertion accepts generators another_list # add text from a list, str.join is used in rendering )) # add tags and content later page[1][0](A(href='www.bar.com')) # calling the tag page[1][0].append(A(href='www.baz.com')) # using the API link = A().append_to( page.body[0]) # access the body as if it's a page attribute page.body( testDiv=Div() ) # WARNING! Correct ordering with named Tag insertion is ensured with Python >= 3.5 (because kwargs are ordered) link.attr(href='www.python.org')( 'This is a link to Python.' ) # Add attributes and content to already placed tags