A package for useful python templates.
Project description
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!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytemplatesforpython-1.1.1.tar.gz
.
File metadata
- Download URL: pytemplatesforpython-1.1.1.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b73131ba4e82a4a8a8b62dfd4edb26ec779e6b76e1434c0bf17411316d62751b |
|
MD5 | 7c8cb2c80be07fb079ef586ccb65e765 |
|
BLAKE2b-256 | 4b6f003c9f78b0186a18a8944b158c6ce9e32a0f4e9a6f4e3170bb7e5d9749a4 |
File details
Details for the file pytemplatesforpython-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: pytemplatesforpython-1.1.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 739ca96b7d76ca67795d8d868feb501446b30efa51b1e022430fe83b6ad9d585 |
|
MD5 | 1a782560c35d561d953f6e9665562d8d |
|
BLAKE2b-256 | be54f0d4498196d8897c51799ea37c272cf69affcb6761f3d08b20d3da0a24a9 |