htmlgen

htmlgen

Kind of like HTMLGen, only much simpler. Like stan, only not. The only important symbol that is exported is html.

You create tags with attribute access. I.e., the A anchor tag is html.a. The attributes of the HTML tag are done with keyword arguments. The contents of the tag are the non-keyword arguments (concatenated). You can also use the special c keyword, passing a list, tuple, or single tag, and it will make up the contents (this is useful because keywords have to come after all non-keyword arguments, which is non-intuitive).

If the value of an attribute is None, then no attribute will be inserted. So:

>>> html.a(href='http://www.yahoo.com', name=None, c='Click Here')
'<a href="http://www.yahoo.com">Click Here</a>'

If a non-string is passed in, then webhelpers.escapes.html_escape is called on the value.

html can also be called, and it will concatenate the string representations of its arguments.

html.comment will generate an HTML comment, like html.comment('comment text', 'and some more text') -- note that it cannot take keyword arguments (because they wouldn't mean anything).

For cases where you cannot use a name (e.g., for the class attribute) you can append an underscore to the name, like html.span(class_='alert').

Examples:

>>> html.html(
...    html.head(html.title("Page Title")),
...    html.body(
...    bgcolor='#000066',
...    text='#ffffff',
...    c=[html.h1('Page Title'),
...       html.p('Hello world!')],
...    ))
'<html><head><title>Page Title</title></head><body text="#ffffff" bgcolor="#000066"><h1>Page Title</h1><p>Hello world!</p></body></html>'
>>> html.a(href='#top', c='return to top')
'<a href="#top">return to top</a>'

Note

Should this return objects instead of strings? That would allow things like html.a(href='foo')('title'). Also, the objects could have a method that shows that they are trully HTML, and thus should not be further quoted.

However, in some contexts you can't use objects, you need actual strings. But maybe we can just make sure those contexts don't happen in webhelpers.


Attributes

a html

<webhelpers.htmlgen.Base instance at 0x16b0b70>

See the source for more information.