Skip to main content

Because the world need's yet another HTML templating framework

Project description

ZHTML (LGPLv3)

Because the world needs another HTML framework.

ZHTML is short for ZacHTML, which is short for Zach HTML.

Install

python -m pip install zhtml

Tutorial

Create a blank HTML page to begin.

<!DOCTYPE html>
<html lang="en-US">

<head>
    <meta charset="utf-8">
    <title>zhtml Example</title>

    <!-- Water.css, to spruce things up -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">

</head>
<body>
    
</body>

</html>

ZHTML is a dynamic rendering library, so instead of putting our HTML directly into our file, we will instead insert a comment as a placeholder.

</head>

<body>
    <!-- APP -->
</body>

</html>

Next, let's begin our script.

# example.py

import zhtml as html


if __name__ == '__main__':
    example = html.Page('examples/example.html', 'index')

html.Page allows us to load our template file as an object.

Before going any further, we need to bind our APP placeholder, using the html.Placeholder class.

# example.py

import zhtml as html


if __name__ == '__main__':
    binding = html.Placeholder('APP')
    example = html.Page('examples/example.html', 'index')

By passing 'APP' to our Placeholder object, we are telling it to look for the <!-- APP --> placeholder in our HTML file. The resulting object is an re.Pattern.

Next, let's create some HTML to inject into our example.html page.

ZHTML uses string functions to assemble HTML snippets. To start, let's create a heading and a paragraph.

# example.py

import zhtml as html


if __name__ == '__main__':
    binding = html.Placeholder.create('APP')
    example = html.Page('examples/example.html', 'index')

    heading = html.heading('Hello, World!')
    paragraph = html.paragraph('This is a body of text.')

Once created, lets wrap our new elements in a main tag. This will be the root of our application. Because ZHTML assembles elements as strings, unioning our heading and paragraph is as easy as concatenating two strings.

# example.py

import zhtml as html


if __name__ == '__main__':
    binding = html.Placeholder.create('APP')
    example = html.Page('examples/example.html', 'index')

    heading = html.heading('Hello, World!')
    paragraph = html.paragraph('This is a body of text.')

    main = html.main(heading + paragraph)

Finally, we can inject our application using the inject_text method on our Page object. Be sure to provide the Placeholder that we created earlier. To output the results, use the write method.

# example.py

import zhtml as html


if __name__ == '__main__':
    binding = html.Placeholder.create('APP')
    example = html.Page('examples/example.html', 'index')

    heading = html.heading('Hello, World!')
    paragraph = html.paragraph('This is a body of text.')

    main = html.main(heading + paragraph)

    example = example.inject_text(main, binding)
    example.write('output')

You should now have a file named index.html in the output folder of your project.

<!DOCTYPE html>
<html lang="en-US">

<head>
    <meta charset="utf-8">
    <title>zhtml Example</title>

    <!-- Water.css, to spruce things up -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">

</head>

<body>
    
    <main>
        <h1>Hello, World!</h1>
        <p>This is a body of text.</p>
    </main>
    
</body>

</html>

index.html


Contributing

ZHTML is a young project, with a lot of work to do. If you want to help contact z3c0, or fork the project and submit your change as a merge request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

zhtml-1.0.16-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file zhtml-1.0.16-py3-none-any.whl.

File metadata

  • Download URL: zhtml-1.0.16-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for zhtml-1.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 764ae44b368f65a2118dfc2a5a8db794e2b6ddfecd859f95e11bbe7daa16f3b2
MD5 238bb475e94bb6af4a95b04a89379997
BLAKE2b-256 dcb25871d37316f0fe789fbbd5c5a8ca808f2d0547bd76fe7fcebc5d8a6c7f37

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page