PyTemplatesForPython Quick Start (beta)
Install module
pip install pytemplatesforpython
Create simple template
Write some text:
Plain text
Add a variable:
Plain text written by {{ insert(name) }}
And an object:
Plain text is written by {{ insert(author.name) }}
Some imformation about him:
He`s {{ insert(author.age )}} years old, he lives in {{ insert(author.city) }}
You ask what else can you do?
I answer — all you can in the simple python!
Several nuances:
- Don`t allow symbols of a new line inside of the double braces ({{}})
- Do you want to use for, while, if, etc? I am just going to show how to do it, now just remember: {{+}} to increase Tabs number, {{-}} to decrease it.
- All of the expressions in double braces are connected, you can write {{a = 3}} first, and then {{insert(a)}}
Plain text is written by {{ insert(author.name) }}
Several facts about him:
{{ for i, fact in enumerate(author.facts): }}{{+}}
Fact в„–{insert(i)}: {{ insert(fact) }}
{{-}}
Can you see it now? Expressions those are between {{+}} and {{-}} will have a tab before them, just like:
for fact in author.facts:
insert(i)
insert(fact)
But this still won't work: all templates basically have just one parameter they can use in expressions - "context". Let's divide it into several parameters:
{{ man: Creature = context["man"] }}
{{ monkey: Creature = context["monkey"] }}
Man loves {{ insert(man.loves) }} and monkey loves {{ insert(monkey.loves) }}.
Man lives in {{ insert(man.lives_in) }}, monkey lives {{ insert(monkey.lives_in) }}.
For now we used templates only for the plain text but the most common usage is for HTML code:
<!DOCTYPE html>
{{ author = context["author"] }}
<html>
<body>
<p>Plain text is written by {{ insert(author.name) }}</p>
<p>Several facts about him:</p>
<ul>
{{ for i, fact in enumerate(author.facts): }}{{+}}
<li>
Fact в„–{{ insert(i) }}: {{ insert(fact) }}.
</li>
{{-}}
</ul>
</body>
</html>
Work with templates
Okay, we've created some templates, let's work with them in Python.
Save one template into template.html and import FileTemplate from the module:
from pytemplatesforpython import FileTemplate
Create a FileTemplate object:
template: FileTemplate = FileTemplate("template.html")
Create an Author class that we used in the template:
class Author:
def __init__(self, name, facts):
self.name = name
self.facts = facts
author = Author("Alexey", ["lives in Israel", "knows Python, Java and JavaScript", "can`t think of more facts"])
Render the template:
result: str = template.render({"author":author})
Congrutilations! Now you can check what did we get,
print(result)
write the result to the some another file,
with open("result.html", "w+") as file:
file.write(result)
or, if you use django, you can make an HttpResponce that every view has to return:
from django.http import HttpResponse
def view(req):
return HttpResponce(template.render({"author":author}))
TemplatesLoader
In real project you usually need to use many templates, as example for different website pages. For that you can use TemplatesLoader. Import it:
from pytemplatesforpython import TemplatesLoader
Create a TemplatesLoader object. You can specify a directory, where are all your templates stored or it will be current working directory:
loader: TemplatesLoader = TemplatesLoader("templates/")
When you create a template, it compiles, in that step some syntax errors can be found. You can compile all your templates at start or compile ones that are being needed. To compile some of templates at start use:
loader.load_template(filename)
Or you can load all the templates from the folder and its subfolders:
loader.recursively_load_folder(filename)
To get one of the templates, never mind, if it was loaded or no use:
loader.get_template(filename)
Please note that all of paths should be satisfied as relative to the path, that has been satisfied when the loader was created.
Good Luck!
Metadata
Release files for pytemplatesforpython 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytemplatesforpython-1.1.1.tar.gz | 9.0 kB | Without provenance | Details for pytemplatesforpython-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytemplatesforpython-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for pytemplatesforpython-1.1.1-py3-none-any.whl |
Total release size: 18.0 kB
Release files / pytemplatesforpython-1.1.1.tar.gz
| Download URL | pytemplatesforpython-1.1.1.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b73131ba4e82a4a8a8b62dfd4edb26ec779e6b76e1434c0bf17411316d62751b
|
|
BLAKE2b-256 checksum How to use checksums |
4b6f003c9f78b0186a18a8944b158c6ce9e32a0f4e9a6f4e3170bb7e5d9749a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.3
|
Release files / pytemplatesforpython-1.1.1-py3-none-any.whl
| Download URL | pytemplatesforpython-1.1.1-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
739ca96b7d76ca67795d8d868feb501446b30efa51b1e022430fe83b6ad9d585
|
|
BLAKE2b-256 checksum How to use checksums |
be54f0d4498196d8897c51799ea37c272cf69affcb6761f3d08b20d3da0a24a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.3
|
Security for pytemplatesforpython 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.