A python template engine
Project description
Codra
Codra is a template engine written in python to render a string given its template and the data to fill this template
Installation
pip install codra
Example
from codra import Template
template = r"""
Hello my name is {{ name }}. I am {{ age }} years old.
This is the activities I do:
{{ for i, activity in enumerate(activities) }}
{{ i }}. {{ activity }}
{{ endfor}}
Those are the type of numbers less than 20
{{ for i in range(20) }}
{{ if is_prime(i) }}
{{i}} is prime.
{{ endif }}
{{ if not is_prime(i) }}
{{i}} is not prime.
{{ endif}}
{{ endfor}}
"""
def is_prime(n):
if n < 2:
return False
elif n == 2:
return False
else:
for i in range(2, n):
if n % i == 0:
return False
return True
print(Template(template).render(name = "Alice", age = 20, activities = ["coding", "playing", "eating"], is_prime = is_prime, enumerate = enumerate))
Format
The template consists mainly of data and executable sections which may be interleaved with each other. The executable sections is surrounded by double curly braces. {{
can be escaped by preceding it with backslash.
The executable sections (from now on called constructs) is divided into three types:
1. Expressions.
2. If conditions.
3. For loops.
The constructs are eventually replaced by text after rendering. Since expressions might contain identifiers and function calls, they're supplied in kwargs to the render function with the names that appear iniside the template.
Language constructs
In this section, the different language constructs is covered in more details.
Expressions
Expressions in codra can be considered as a subset of the expressions in python. It's worth noting that the semantic analysis is based on that of python. The expressions is parsed and computed using the corresponding construct in python. That might explain a lot of the behaviour of the templare engine.
Literals
String literals are surrounded by either single quotation marks, or double quotation marks.
Example:
'hello world'
Numeric literals is any consecutive digits (only Integers is supported for now). Despite the fact that only integers is supported as literals, the result of any expression can be either integer of float.
1984
Boolean literals is planned to be added with the case sensitive keywords True and False.
Arithmetic operators
The operators +
, -
, /
, *
and %
have the same semantic meaning as they do in python. Which means that +
can be used also to concatanate strings.
Example:
1 + 2 * 6 / 4 % 5
evaluates to 4
Comparison operators
You can use ==
, !=
to check for equality and <
, <=
, >
and '>=` to compare different expressions
1 < 4
is True
Logical operators
and
, or and
not` can be used to combine the result of comparison expressions.
Example:
1 == 2 or not 3 == 4
evaluates to True
Access operators
Access operators are used to fetch the value from an object or a dictionary.
.
is used to access class members of an object.
[]
is used to get the value associated with a key in a dict.
Example:
person.favorite_food['morning']
Function calls
The built in functions len
and range
is supplied by default. To use another functions they must be supplied in the arguments of render. To call a function use its name along with a list of possibly empty parameters separated by ,
and enclosed in parentheses.
Example:
Template("{{ is_odd(1) }}").render(is_odd = lambda x : x % 2 == 1)
evaluates to "True"
If conditions
If conditions are used to print the enclosing data only if the expression evaluates to True:
The Syntax of if condition is:
{{ if _condition_ }}
_body_
{{ endif }}
The body of the if can be any valid template (data, constructs or both). The if construct evaluates to its body if the condition is True, Otherwise, it's replaced by an empty string.
Example:
Template("""
This is a condition to greet Alice only.
{{ if name == "Alice" }}
Hello {{ name }}.
{{ endif }}
""").render(name = "Alice")
this evaluates to:
"""
This is condition to greet Alice only.
Hello Alice.
"""
For loops
Loops can be used to iterate over any iterable python object (e.g. lists).
Loops can have one or more variables. It's translated by assigning the variable(s) to each item in the given iterable and evaluating the body of the loop. Then the result of each iteration is combined into a single unit.
Examples:
Template("""
{{ for i in range(len(lst)) }}
{{i + 1}}. {{ lst[i] }}
{{ endfor }}
""").render(lst = ['a', 'b', 'c'])
which is equivalent to:
Template("""
{{ for i, e in enumerate(lst) }}
{{i + 1}}. {{ e }}
{{ endfor }}
""").render(lst = ['a', 'b', 'c'], enumerate = enumerate)
evaluates to:
"""
1. a
2. b
3. c
"""
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 codra-0.0.3.tar.gz
.
File metadata
- Download URL: codra-0.0.3.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74685b2336dbfaf702783a7dfc13e97b16ac8f70a79f3e6ed70c26c6150cb9be |
|
MD5 | 745b6bac4fc9a6d8f43c0088da75d15e |
|
BLAKE2b-256 | 7b3a912a2d33049f09a5a1085914aa4df825b8ff4b7a9288d10173b4a1218919 |
File details
Details for the file codra-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: codra-0.0.3-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44e127453da76fd9536d48dd3d865e16bd230e25717654cedd2e645619234484 |
|
MD5 | 20987b976c73b1d20c61b5214a436902 |
|
BLAKE2b-256 | d7bbd00f2d912197b320ea1393025cb5860fbf347bc4b652d8c15967602f0f9f |