-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempyTest.py
31 lines (27 loc) · 1.43 KB
/
tempyTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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(test=Div()) # WARNING! Correct ordering with named Tag insertion is ensured with Python >= 3.5 (because kwargs are ordered)
page[1][0].append(A(href='www.baz.com')) # using the API
link = Link().append_to(page.body) # access the body as if it's a page attribute
link.attr(href='www.python.org')('This is a link to Python') # Add attributes and content to already placed tags
page.render() # does not return on Python shell
testfile = open("tempyTest.html", "w")
print(page.render(), file=testfile)
testfile.close()