Skip to main content

A lightweight Python template engine

Project description

Blockie Python template engine

Blockie is an extremely lightweight and simple universal Python-based template engine. It can generate various types of text-based content, e.g., standard text, source code, data files or markup language content like HTML, XML or markdown.

Blockie is aimed to be used in template-based projects that do not need templates containing complex custom commands or programming language parsers. Instead, Blockie uses only a few simple but extremely multipurpose principles and clean, logicless templates. If a more advanced template-filling logic is needed, then it is expected to be defined directly within the user-defined Python script, which avoids the need for a custom template language.

The block diagram below illustrates the fairly standard process of generating the content from a template using values defined in the input data:

    +----------+   +------------+
    | template |   | input data |
    +----------+   +------------+
          |              |
          V              V
      +-----------------------+
      | Python filling script |
      |     using blockie     |
      +-----------------------+
                  |
                  V
        +-------------------+
        | generated content |
        +-------------------+

Please read the full documentation here.

Installation

The Blockie package can be installed from the Python Package Index using the following pip console command:

pip install blockie

Alternatively, it is also possible to install the Blockie package from a *.tar.gz source distribution that can be downloaded from the dist directory:

pip install blockie-<version>.tar.gz

Quick start

The following Python script serves as a simple illustration of all basic principles. The template is loaded from the template string and filled using the data dictionary with the FLAG variable in the template defined by the script since the Blockie templates are logicless. At the end the generated content is printed out.

    import blockie


    template = """
                                SHOPPING LIST
      Items                                                         Quantity
    ------------------------------------------------------------------------
    <ITEMS>
    * <FLAG>IMPORTANT! <^FLAG>MAYBE? </FLAG><ITEM><+>               <QTY><UNIT> kg<^UNIT> l</UNIT>
    </ITEMS>


    Short list: <ITEMS><ITEM><.>, <^.></.></ITEMS>
    """

    important_items = ("potatoes", "rice")
    maybe_items = ("cooking magazine",)

    data = {
        "items": [
            {"item": "apples", "qty": "1", "unit": 0},
            {"item": "potatoes", "qty": "2", "unit": 0},
            {"item": "rice", "qty": "1", "unit": 0},
            {"item": "orange juice", "qty": "1", "unit": 1},
            {"item": "cooking magazine", "qty": None, "unit": None}
        ]
    }

    for item in data["items"]:
        item["flag"] = 0 if item["item"] in important_items else 1 if item["item"] in maybe_items else None

    blk = blockie.Block(template)
    blk.fill(data)
    print(blk.content)

Prints the following generated content:

                            SHOPPING LIST
  Items                                                         Quantity
------------------------------------------------------------------------
* apples                                                        1 kg
* IMPORTANT! potatoes                                           2 kg
* IMPORTANT! rice                                               1 kg
* orange juice                                                  1 l
* MAYBE? cooking magazine                                       


Short list: apples, potatoes, rice, orange juice, cooking magazine

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

blockie-1.0.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

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

blockie-1.0.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file blockie-1.0.0.tar.gz.

File metadata

  • Download URL: blockie-1.0.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for blockie-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c053ceb7b8d10aa337daeebf8404878b10b3b1cedd9806e7eee252593e9c3f08
MD5 38a060016e64ddec5b465a1a1855f762
BLAKE2b-256 966e99173e2d12dfe432f5a516cfc02b1961114b6b5859d7a913da6528039b6e

See more details on using hashes here.

File details

Details for the file blockie-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: blockie-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for blockie-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3317d49f6de7d1ede8a9953c7209b06a9ed5b4bde431a44ab75c79bfe19e5603
MD5 46decd1293135741711acfce8b70e12c
BLAKE2b-256 86d63d60345b416dcfce9269ebdeb2681083836d018a7ed4179a674f99046860

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