Template Engine

Pytonik provides flexible templating engine, making use of blocks, Variables , Loops, conditionals statement , operators , iteration and scopes.

from pytonik.App import App
app = App()

items = [
        dict(name="dog", age=5),
        dict(name="cat", age=2),
        dict(name="snake", age=26),

    ]

data = {
    'items': items,
    'my_var':  "Welcome to Pytonik",
    'my_var_2': "Nothing",
    'status': "active",
    'num': 1,

    }


app.views('index', data)

Variables

<div>{{my_var}}</div>

Block

Type of block if, each and call.

Loops

loop with dictionary or json

{% each items %}
    <div>{{it.name}}  {{it.age}}</div>
{% endeach %}

loop with list

{% each [1,2,3] %}
    <div>{{it}}</div>
{% endeach %}

Loop items has iteration with a scope, to access attributes which is a parent context or outer variable use ..

{% each items %}
    <div>{{..status}}</div><div>{{it.name}}  {{it.age}}</div>
{% endeach %}

Conditionals

Supported operators are: >, >=, <, <=, ==, !=. You can also use conditionals with things that evaluate to truth. if conditional statement

{% if num %}
    {{my_var}}
{% endif %}

if conditional statement with else

{% if num %}
    {{my_var}}
{% else %}
    {{my_var_2}}

{% endif %}

if conditional statement with operator

{% if num == 0 %}

    {{my_var}}

{% endif %}

Callable

call block, get or passed positional or keyword arguments or parameter. url is class and path is method

{% call url 'path' %}

url is class and url is method while path='' is keyword arguments or parameter

{% call url 'url' path='' %}