One
Abso-fucking-lutely
======== Tutorial ======== ``generate-html`` is a library to help you generate dynamic HTML using Python. Let's start with a simple example:: from generate_html import fragment, tag, render_html @fragment def say_hello(who): yield 'Hello ' yield tag.b(who) yield '!' print(render_html(say_hello('World'))) This should print the following: .. code-block:: html Hello World! Hopefully that makes some sense to you. It might seem strange to use ``yield`` in this way. The reason for that is that it allows us to use regular flow control to create dynamic HTML fragments and documents:: @fragment def show_friends(friend_list): if friend_list: yield 'Your friends are:' for friend in friend_list: yield tag.button(friend) else: yield tag.i('You have no friends :(') Then you can give this a list of functions:: print(render_html(show_friends(['Joey', 'Monica', 'Phoebe']))) Which prints: .. code-block:: html Your friends are: On the other hand, if you pass an empty list:: print(render_html(show_friends([]))) ... you should get: .. code-block:: html You have no friends :( What if you want to have nested tags, though? ``for`` loops are statements, so you can't pass it as an argument. For this reason, elements are also usable as context managers:: @fragment def show_friends_2(friend_list): if friend_list: with tag.ul(): for friend in friend_list: yield tag.li(friend) else: yield tag.i('You have no friends :(') Now this:: print(render_html(show_friends_2(['Joey', 'Monica', 'Phoebe']))) ... will produce: .. code-block:: html
One
Abso-fucking-lutely