Module rattlepy.templating : Rattle.py API Reference

Templating class and functions.

rattlepy.templating.escapeHtmlEntities(string)

Escapes certain characters.

class rattlepy.templating.Element(tag, *, className=None, **kwargs)

A class of an HTML element which is able to have children.

Usage:

with Element(tagname, attributes...):
  ...

For class attribute, you can use “className” instead of using “class” directly. Or, also you can use the way:

with Element(tagname, **{'class': 'my-class'}):
  ...

Attributes which are invalid identifier in Python like data- are also available in the way.

exposes(element=None)

Changes parent element dynamically. This function aims creating custom component more easily.

Code example:

with Element("hoge") as hoge:
  # this element will be a child of hoge
  with Element("some-inner") as inner:
    hoge.exposes(inner)

with hoge:
  # this element will be a child of some-inner
  with Element("other-element"):
    ...
  hoge.exposes()

with hoge:
  # this element will be a child of hoge
  with Element("some-other-element"):
    ...
serialize(formatter='human_friendly', force_add_doctype=False)

Serializes HTML elements. If you want to serialize to minified form, use str(elem).

formatter argument is one of [“human_friendly”, “minify”]. default is “human_friendly” force_add_doctype argument is set whether force add doctype declaration even if the element is not a <html>

class rattlepy.templating.SelfClosedElement(tag, *, _outer=2, className=None, **kwargs)

A class of an HTML element which is unable to have children like img or hr.

Usage:

with Element("hoge"):
  SelfClosedElement(tagname, attributes...)
addself(*, outer=1)

Add self to certain parent node.

rattlepy.templating.text(content)

This function is create text nodes. A string is expected for content argument.

Multiline contents are available in the way:

with Element("hoge"):
  text('''\
  |some
  |multiline
  |text''')

Any characters before | are ignored as spacers. If ending position of line spacers is not specified, all texts are inserted as text nodes.

All dangerous characters (& < > ") will be escaped. If you don’t need the feature, Use rtext instead of this.

rattlepy.templating.node(tag, **kwargs)

Create Element and return it. Equivalent to Element(tag, attributes...).

rattlepy.templating.closed(tag, **kwargs)

Create SelfClosedElement and return it. Equivalent to SelfClosedElement(tag, attributes...).