Skip to main content

A templating engine inspired by Ruby's Slim template engine

Project description

Trim-Template

trim-template is an HTML templating engine for Python inspired by Ruby's Slim template engine. The objective behind Trim is to simplify template syntax to a minimal format that, like Python itself, makes use of indentation to indicate how blocks of code should be interpreted.

Installation

pip install trim-template

Example Template

doctype strict

html
  head
    title My HTML title

    stylesheet src='/some.css'

    javascript:
      console.log('embedded JS inside the template');

  body
    css:
      .alert { color: 'red'; }

    .menu-bar
      - if user.logged_in
        img src={user.profile.image_path}
      - else
        a#login-button.btn.btn-primary href={login_path} Login

    .alert
      h1 {greeting}

    p.exciting This is the first ever Python Trim-Template

    h2#member-list Members

    form
      input type='checkbox' disabled=True checked=True

    p
      ul
        - for user in users
          li
            / code comment - show the user's names. This line will not render.
            span {user.first_name} {user.last_name}
    /! render the footer
    #footer Thanks for using Trim!

Rendered HTML

Trim-Template will render the above template into HTML, as below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
    <head>
        <title>My HTML title</title>

        <stylesheet src="/some.css"></stylesheet>

        <script type='javascript'>
            console.log('embedded JS inside the template');
        </script>
    </head>

    <body>
        <style>
          .alert { color: 'red'; }
        </style>

        <div class="menu-bar">
            <a class="btn btn-primary" href="/auth/login" id="login-button"/>Login</a>
        </div>

        <div class="alert">
            <h1>Hello World!</h1>
        </div>

        <p class="exciting">
            This is the first ever Python Trim-Template
        </p>

        <h2 id='member-list'>Members</h2>

        <form>
          <input type="checkbox" disabled="disabled" checked="checked"/>
        </form>

        <p>
            <ul>
                <li>
                    <span>Stephen Colber</span>
                </li>
                <li>
                    <span>Bob Marley</span>
                </li>
                <li>
                    <span>Charlie Chaplin</span>
                </li>
            </ul>
        </p>
        <!-- render the footer -->
        <div id='footer'>Thanks for using Trim!</div>
    </body>
</html>

Using Trim

from trim_template.trim import TrimTemplate

tmpl = TrimTemplate("file.html.trim")
tmpl.set('login_path', '/auth/login')
tmpl.set('greeting', 'Hello World!')
tmpl.set('users', users)

output = tmpl.render()
print(output)

Where file.html.trim (also in the examples dir) contains the following.

Options

Option Values Description
debug all / tags debug output format when calling tmpl.debug()
pretty True / False output pretty HTML
indentation integer depth of indentation for debugging output

Initialization parameters

TrimTemplate can be initialized with multiple parameters, the full set shown below:

tmpl = TrimTemplate('file.html.trim', pretty=True, debug='all', indent=4, vars={greeting: 'hello'})

Syntax

See the USAGE markdown file for details on trim syntax and other usage.

Contributing

Contributions are welcome. Fork the project and create a pull request.

Authors

David Kelly created the project in Feb 2024

Project details


Download files

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

Source Distribution

trim_template-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trim_template-1.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file trim_template-1.0.1.tar.gz.

File metadata

  • Download URL: trim_template-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Linux/5.15.0-141-generic

File hashes

Hashes for trim_template-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cb78613dbbcb4b2a4f34019e50bb112f4532c80c7de48c9854900d3b7d8033ef
MD5 d5e6c0c5e7a43adc0e30e31131c239b6
BLAKE2b-256 fba2837bbaa2c46167acf0efedda96a91aaad3604dec1404fca5f2a5e5b198bf

See more details on using hashes here.

File details

Details for the file trim_template-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: trim_template-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Linux/5.15.0-141-generic

File hashes

Hashes for trim_template-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 005f686e0d47b9e1e6d9e537255bb20dbe657b41294ec3c00c630320f594cc25
MD5 1588bc822cdea9ac3149c1bc4df78380
BLAKE2b-256 eda1bc8c4201e68fcf5ba1ead874af8064247f518b0d1d8510ca24afd4584b8b

See more details on using hashes here.

Supported by

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