Skip to main content
https://img.shields.io/pypi/v/xmltag.svg https://travis-ci.org/zenwalker/python-xmltag.svg https://coveralls.io/repos/github/zenwalker/python-xmltag/badge.svg

XMLTag — tool for easy creating XML and HTML documents in the Python style. Idea was taken from yattag, but xmltag offers an improved features with less code (really, api is very small, just see source code).

Installation

$ pip install xmltag

Usage example

from xmltag import HTMLDocument
doc = HTMLDocument()

with doc.head():
    doc.title('Document')

with doc.body():
    doc.h1('Helo world!', class_="heading")
    users = ['Marry', 'John', 'Bob']
    with doc.ul(id='user-list'):
        for name in users:
            doc.li(name)

print(doc.render())

More examples avaliable in examples directory.

Different syntaxes

You can write different XML-like documents, such as XML, HTML or XHTML. Just use one of these classes: XMLDocument, HTMLDocument or XHTMLDocument.

from xmltag import HTMLDocument, XHTMLDocument, XMLDocument

xhtml_doc = XHTMLDocument()
xml_doc.input(name="email")
print(xml_doc.render())  # <input name="email" />

html_doc = HTMLDocument()
html_doc.input(name="email")
print(xml_doc.render())  # <input name="email">

Layouts

You can create layouts for reusing code.

class PageLayout(Layout):
    def setup_document(self):
        return HTMLDocument()

    def setup_layout(self, doc):
        with doc.head():
            with doc.title():
                self.define('title')
  1. Define setup_document method and return document instance from it.

  2. Write layout in setup_layout method and define placeholders using self.define method.

Following this actions you can inherit from PageLayout and define render_title method.

class Page(PageLayout):
    def render_title(self, doc):
        doc.text('Hello World!')

Then call Page().render(). All placeholders that are defined in layout class will be filled with content.

Escaping

XMLTag provide escaping content by default. Add safe=True if you don’t need it.

doc.div(unescaped_text, safe=True)

Metadata

Release files for xmltag 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmltag 1.3.0
File Size Uploaded Provenance Actions
xmltag-1.3.0.tar.gz 4.0 kB Without provenance Details for xmltag-1.3.0.tar.gz

Release files / xmltag-1.3.0.tar.gz

Download URL xmltag-1.3.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b2c06a806ba732afd6c09cd979025ebe7d2d3e0990d352fa9d4f25fd33d5fd34
BLAKE2b-256 checksum
How to use checksums
553fd912e10a2ad18c692947306bfe2f016eed377af920eb2b64251f8a33b43c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for xmltag 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page