Skip to content

Complete Example

Carl Smith edited this page Jun 10, 2018 · 2 revisions

This code example is the entire content of a module, taken from a real project. It freezes four simple webpages (for handling auth errors, 404 errors etc):

from htme import *

subpages = {
    "unknown": {
        "type": 'Unknown User',
        "body": 'Your <a href="{URL}">account</a> is unknown.'
    },
    "denial": {
        "type": 'Unauthorized User',
        "body": 'Your rank is too low to access this part of the app.'
    },
    "login": {
        "type": 'Anonymous User',
        "body": 'You must <a href="{URL}">log in</a> to your account.'
    },
    "deadend": {
        "type": 'File Not Found',
        "body": 'This is a deadend. Go to the <a href="/">homepage</a>?'
    }
}

description = "A subpage for {[type]} errors."

docs = Engine(favicon="/static/favicon.png")
docs.install(Style("/static/subpage.css"))

error_span, message_paragraph = SPAN(), P()

docs *= HEADER(Anchor("/", "Write Dot Run"))
docs *= MAIN(H1("ERROR: ", error_span), message_paragraph)
docs *= FOOTER(
    DIV(Anchor("/terms", "Terms of Use")), SPAN("|"),
    DIV(Anchor("/privacy", "Privacy Policy"))
)

for key, subpage in subpages.items():

    docs.title = subpage["type"]
    docs.description = description.format(subpage)
    error_span /= subpage["type"]
    message_paragraph /= subpage["body"]
    docs.freeze(key)

You could now import docs from the module above, and then do stuff like this (which retrieves a copy of one of the frozen docs, interpolating the URL in the process):

docs("login", URL=get_login_url())
Clone this wiki locally