Skip to main content

A minimalistic python templating library

Project description

mould - A minimalistic templating engine for Python

This Python library helps to render HTML pages and plain text with variable substitutions, if conditions and for loops.

Templating syntax :

To delcare variables enclose the variable name with '{{ ' and ' }}'

Hello {{ text }} -- 'text' is the variable here

You can use the same syntax to write variable expressions.

This is line number {{ n+10 }}

if conditional


Use '{% ' and ' %}' to enclose the lines where an if block starts and use the same with 'endif' keyword for closing the if blocks
{% if 5 < 6  %}
Hello world!
{% endif %}

Variables can also be used to write if conditions. Inside the conditional statement, the variables can be written as such. The enclosing syntax mentioned above is not required.

{% if x > y %}
Hello Python!
{% endif %}

if block supports 'else' and 'elif' as well.

{% if x > y %}
Hello X!
{% elif z > x %}
Hello Z!
{% else %}
Hello ABC!
{% endif %}

for loop

Use '{% ' and ' %}' to enclose the lines where a 'for' block starts and use the same with 'endfor' keyword for closing the 'for' blocks

{% for n in numbers %}
This is line {{ n }} printed using for loop
{% endfor %}

In the above code, 'n' and 'numbers' are variables where 'n' is the looping variable and 'numbers' is the list on whose items, the loop is run.

for supports looping over lists, strings and dictionaries

How to install:

You can get this from PyPI using pip

pip install mould

Alternatively, you can get it from git also using pip.

pip install https://github.com/ramk95/mould-it/archive/refs/heads/master.zip

How to use:

Import the library to your python program

import mould

Call the mould.it() function with your input file / input text along with a dictionary of variables that you have used in your text / input file

mould.it(input_text, variables_dictionary)

This will return the rendered output. You can either print it direclty or save it.

print(mould.it(input_text, variables_dictionary))

or

rendered_data = mould.it(input_text, variables_dictionary)

Dependencies:

mould uses python standard libraries io and os. No other dependent packages are required.

Example:

    import mould

    input = "Hello {{ var }}"
    variables = {"var" : "world !"}

    print(mould.it(input,variables))



    Output:
    Hello world !

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

mould-0.0.7.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file mould-0.0.7.tar.gz.

File metadata

  • Download URL: mould-0.0.7.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for mould-0.0.7.tar.gz
Algorithm Hash digest
SHA256 674ea564bb6bb17e380e4b9f186d6adae3567e523d866fcddaf1abf65bdd8c42
MD5 6972d1e14b0526b2946fb1f7e1e159c7
BLAKE2b-256 961fe7d1084703e486fa766a05fc93f37ed84b500e58d0efe7a0e7a58b0ec0ea

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