Create DOM elements by instantiating tag classes. Those elements are nodes in the DOM tree and can be attached, detached, moved and composed together dynamically.
There are 2 other ways to create TemPy objects:
page = Html() div = Div() new_page = page.clone() new_page_2 = page + div new_page_3 = new_page_2 - div list_of_5_divs = div * 5
TemPy provides a class for every HTML5 element defined in the W3C reference, those classes can be imported from the
Each Tempy Tag class is either a
tempy.elements.Tag with a start tag and an end tag, that can contain something, or a
tempy.elements.VoidTag that can not contain other things and is composed of a single html tag mark.
from tempy.tags import Div, Br Div.render() # Subclass of tempy.elements.Tag >>> <div></div> Br.render() # Subclass of tempy.elements.VoidTag >>> <br/>
A few TemPy tags have custom behavior:
tempy.tags.Commenttag needs as first argument the comment string:
Comment('This is my comment')
tempy.tags.Doctypetag needs as first parameter a doctype code (
Doctype('html_frameset')) to choose between:
tempy.tags.Htmltag accepts the keyword argument “doctype”, so it adds a Doctype tag before himself (default is “HTML” doctype):
tempy.tags.Atag with nothing inside it will render himself with the href string inside it:
A(href='www.link.com').render() -> <a href="www.link.com">www.link.com</a>