README.rst
1.. image:: https://travis-ci.org/leforestier/yattag.svg
2 :target: https://travis-ci.org/leforestier/yattag
3
4Why use a template engine when you can generate HTML or XML documents with Python in a very readable way?
5
6( full tutorial on yattag.org_ )
7
8Basic example
9-------------
10
11Nested html tags, no need to close tags.
12
13.. code:: python
14
15 from yattag import Doc
16
17 doc, tag, text = Doc().tagtext()
18
19 with tag('html'):
20 with tag('body', id = 'hello'):
21 with tag('h1'):
22 text('Hello world!')
23
24 print(doc.getvalue())
25
26
27Html form rendering
28-------------------
29
30Yattag can fill your HTML forms with default values and error messages.
31Pass a ``defaults`` dictionnary of default values, and an ``errors`` dictionnary of error messages to the ``Doc`` constructor.
32Then, use the special ``input``, ``textarea``, ``select``, ``option`` methods when generating your documents.
33
34
35Example with default values
36~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
38.. code:: python
39
40 from yattag import Doc
41
42 doc, tag, text = Doc(
43 defaults = {'ingredient': ['chocolate', 'coffee']}
44 ).tagtext()
45
46 with tag('form', action = ""):
47 with tag('label'):
48 text("Select one or more ingredients")
49 with doc.select(name = 'ingredient', multiple = "multiple"):
50 for value, description in (
51 ("chocolate", "Dark chocolate"),
52 ("almonds", "Roasted almonds"),
53 ("honey", "Acacia honey"),
54 ("coffee", "Ethiopian coffee")
55 ):
56 with doc.option(value = value):
57 text(description)
58 doc.stag('input', type = "submit", value = "Validate")
59
60 print(doc.getvalue())
61
62Example with default values and errors
63~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64
65.. code:: python
66
67 from yattag import Doc
68
69 doc, tag, text = Doc(
70 defaults = {
71 'title': 'Untitled',
72 'contact_message': 'You just won the lottery!'
73 },
74 errors = {
75 'contact_message': 'Your message looks like spam.'
76 }
77 ).tagtext()
78
79 with tag('h1'):
80 text('Contact form')
81 with tag('form', action = ""):
82 doc.input(name = 'title', type = 'text')
83 with doc.textarea(name = 'contact_message'):
84 pass
85 doc.stag('input', type = 'submit', value = 'Send my message')
86
87 print(doc.getvalue())
88
89Full tutorial on yattag.org_
90
91GitHub repo: https://github.com/leforestier/yattag
92
93.. _yattag.org: https://www.yattag.org
94