Skip to content
rojalatorPublic
forked from leforestier/yattag

About

Python library to generate HTML or XML in a readable, concise and pythonic way.

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Latest commit

 

History

151 Commits

Folders and files

Repository files navigation

This version:-

  1. Allows +=
from yattag import Doc

doc, tag, text = Doc().tagtext()
doc2 = Doc()

with doc2.tag('p'):
    doc2.text('This is some text')
with tag('html'):
    with tag('body', id = 'hello'):
        with tag('h1'):
            text('Hello world!')
    doc += doc2

print(doc.getvalue())

'<html><body id="hello"><h1>Hello world!</h1><p>This is some text</p></body></html>'
  1. Has a comment() function
from yattag import Doc

doc, tag, text = Doc().tagtext()
doc.comment('This is a comment')

3. Can automatically convert Python reserved-name attribute names (such as 'for') if they have a leading underscore, meaning less use is needed of doc.attr()

from yattag import Doc

doc, tag, text = Doc().tagtext()
doc.line('label', text_content='Name', _for='name')

print(doc.getvalue())

'<label for="name">Name</label>'

4. Can automatically convert attribute names with underscores to HTML names with dashes, again, less use is needed of doc.attr()

from yattag import Doc

doc, tag, text = Doc().tagtext()
with tag('section', klass="map", aria_label='Map', data_index=i, aria_hidden='false', tabindex=i):
    text('A map goes here...')

print(doc.getvalue())

'<section class="map" aria-label="Map" data-index="0" aria-hidden="false" tabindex="0">A map goes here...</section>'

Original Text

Why use a template engine when you can generate HTML or XML documents with Python in a very readable way?

( full tutorial on yattag.org )

Basic example

Nested html tags, no need to close tags.

from yattag import Doc

doc, tag, text = Doc().tagtext()

with tag('html'):
    with tag('body', id = 'hello'):
        with tag('h1'):
            text('Hello world!')

print(doc.getvalue())

Html form rendering

Yattag can fill your HTML forms with default values and error messages. Pass a defaults dictionary of default values, and an errors dictionary of error messages to the Doc constructor. Then, use the special input, textarea, select, option methods when generating your documents.

Example with default values

from yattag import Doc

doc, tag, text = Doc(
    defaults = {'ingredient': ['chocolate', 'coffee']}
).tagtext()

with tag('form', action = ""):
    with tag('label'):
        text("Select one or more ingredients")
    with doc.select(name = 'ingredient', multiple = "multiple"):
        for value, description in (
            ("chocolate", "Dark chocolate"),
            ("almonds", "Roasted almonds"),
            ("honey", "Acacia honey"),
            ("coffee", "Ethiopian coffee")
        ):
            with doc.option(value = value):
                text(description)
    doc.stag('input', type = "submit", value = "Validate")

print(doc.getvalue())

Example with default values and errors

from yattag import Doc

doc, tag, text = Doc(
    defaults = {
        'title': 'Untitled',
        'contact_message': 'You just won the lottery!'
    },
    errors = {
        'contact_message': 'Your message looks like spam.'
    }
).tagtext()

with tag('h1'):
    text('Contact form')
with tag('form', action = ""):
    doc.input(name = 'title', type = 'text')
    with doc.textarea(name = 'contact_message'):
        pass
    doc.stag('input', type = 'submit', value = 'Send my message')

print(doc.getvalue())

Full tutorial on yattag.org

GitHub repo: https://github.com/leforestier/yattag

About

Python library to generate HTML or XML in a readable, concise and pythonic way.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages