Python template engine.
Project description
Backslant
=========
Is a template engine built in completely other way then before.
First - you can use all python and you must use it if you want something more
then just tags.
Second - it completely iterative. You can feed iterators or generators as input and get iterative output.
Third - it works through imports. If you want to get template just import it and use. If you want include
other template - import it. If you want template in some dir, import it! Like ```from . import other_template```.
Use absolute or relative imports.
So, with this principles in mind, you can try backslant:
import backslant
# you need create __init__.py in templates folder to make this work
sys.meta_path.insert(0, backslant.BackslantFinder())
from mypkg.templates.home import index
for chunk in index.render(title='The Real Thing'):
print(chunk)
Or, if you want send rendered html to browser:
from backslant import to_string
to_string(index.render(title='The Real Thing'))
And templates/home/index.bs:
html
head
title
= options['title']
body
div.content#content
h1#header "Header"
You can define a function:
- def render_form(method):
form(method=method)
input(type="text" value="123")
End call it:
h1
== render_form('POST')
Yes, its this simple, you just use python constructions. There `==` is shotrcut for `- yield from`.
And `=` is a shortcut for `- yield`.
And for now inheritance of templates you can made just with function.
base.bs:
!doctype/ html
html
head
title "Page Title"
body
h1{'class': ' '.join(['main', 'content'], 'ng-app': 'Application'}
| Page Header
div.content
== options['content_block']()
div.footer
| Backslant © 2015
index.bs:
- from . import base
- def content():
- for i in range(10):
p
= 'Paragraph {}'.format(i)
== base.render(content_block=content)
I think about adding something like ruby blocks or something to made this a bit more simpler, but
what can be more simple then functions define and call?
But we have syntax sugar for this:
:call base.render(*options)
:content_block
- for i in range(10):
p
= 'Paragraph {}'.format(i)
:footer_block
p "Index page"
Arguments
---------
To define tag arguments you can use arg=`parentised python expression or variable name` or
`tag.class {'a': 5, 'b': ' '.join(options.classes)}` form.
Render or not render?
---------------------
When template compiled, we need it to place in module somehow. If you have any tags or calls in top level,
then we definitely must place them into function. And we create `render` function for this purpose.
Then you import template and call this `render`.
But if you have not in top level, then will yield anything, then function is not needed - you can create library file.
So - if you template on top level only defines functions and imports, then backslant will not implicitly cover
it in `render` function, and this is the way to define your template libs. You can even distribute it on PyPi.
Flask
-----
If you want to integrate backslant into existing project, it can be painful to rewrite all templates. So
we have workaround:
from backslant.flask import extend_jinja2, include_jinja2
And call it in template:
- from backslant.flask import extend_jinja2, include_jinja2
:call extend_jinja2('layouts/base.html')
- def content(ctx):
== include_jinja2('layouts/header.html')
div.container
div.page-header
h1
= options['company'].alias
Afterwords
----------
I have completed examples with flask and http.server in examples folder. And you can compare perfomance with jinja2.
Its almost equal.
I will complete feature set soon, stay tuned.
=========
Is a template engine built in completely other way then before.
First - you can use all python and you must use it if you want something more
then just tags.
Second - it completely iterative. You can feed iterators or generators as input and get iterative output.
Third - it works through imports. If you want to get template just import it and use. If you want include
other template - import it. If you want template in some dir, import it! Like ```from . import other_template```.
Use absolute or relative imports.
So, with this principles in mind, you can try backslant:
import backslant
# you need create __init__.py in templates folder to make this work
sys.meta_path.insert(0, backslant.BackslantFinder())
from mypkg.templates.home import index
for chunk in index.render(title='The Real Thing'):
print(chunk)
Or, if you want send rendered html to browser:
from backslant import to_string
to_string(index.render(title='The Real Thing'))
And templates/home/index.bs:
html
head
title
= options['title']
body
div.content#content
h1#header "Header"
You can define a function:
- def render_form(method):
form(method=method)
input(type="text" value="123")
End call it:
h1
== render_form('POST')
Yes, its this simple, you just use python constructions. There `==` is shotrcut for `- yield from`.
And `=` is a shortcut for `- yield`.
And for now inheritance of templates you can made just with function.
base.bs:
!doctype/ html
html
head
title "Page Title"
body
h1{'class': ' '.join(['main', 'content'], 'ng-app': 'Application'}
| Page Header
div.content
== options['content_block']()
div.footer
| Backslant © 2015
index.bs:
- from . import base
- def content():
- for i in range(10):
p
= 'Paragraph {}'.format(i)
== base.render(content_block=content)
I think about adding something like ruby blocks or something to made this a bit more simpler, but
what can be more simple then functions define and call?
But we have syntax sugar for this:
:call base.render(*options)
:content_block
- for i in range(10):
p
= 'Paragraph {}'.format(i)
:footer_block
p "Index page"
Arguments
---------
To define tag arguments you can use arg=`parentised python expression or variable name` or
`tag.class {'a': 5, 'b': ' '.join(options.classes)}` form.
Render or not render?
---------------------
When template compiled, we need it to place in module somehow. If you have any tags or calls in top level,
then we definitely must place them into function. And we create `render` function for this purpose.
Then you import template and call this `render`.
But if you have not in top level, then will yield anything, then function is not needed - you can create library file.
So - if you template on top level only defines functions and imports, then backslant will not implicitly cover
it in `render` function, and this is the way to define your template libs. You can even distribute it on PyPi.
Flask
-----
If you want to integrate backslant into existing project, it can be painful to rewrite all templates. So
we have workaround:
from backslant.flask import extend_jinja2, include_jinja2
And call it in template:
- from backslant.flask import extend_jinja2, include_jinja2
:call extend_jinja2('layouts/base.html')
- def content(ctx):
== include_jinja2('layouts/header.html')
div.container
div.page-header
h1
= options['company'].alias
Afterwords
----------
I have completed examples with flask and http.server in examples folder. And you can compare perfomance with jinja2.
Its almost equal.
I will complete feature set soon, stay tuned.
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
backslant-0.3.8.tar.gz
(10.6 kB
view details)
Built Distribution
backslant-0.3.8-py3-none-any.whl
(15.9 kB
view details)
File details
Details for the file backslant-0.3.8.tar.gz
.
File metadata
- Download URL: backslant-0.3.8.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d62756f33fd6a9bdce5170aa7bd0f1e9b39ef54135071583c11f215c78bb9582 |
|
MD5 | 22f7a8719b2e1928d9d0999e262ab0ca |
|
BLAKE2b-256 | 5fbfaf9c04b3861decda8c20063d862e3bf8b92376f8e8fe485bdcfd423f8207 |
File details
Details for the file backslant-0.3.8-py3-none-any.whl
.
File metadata
- Download URL: backslant-0.3.8-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ca7d7cefd863d6e3c493c7abdc5ff9ac8a15d820071ef1ffb510ad2d280cc90 |
|
MD5 | 55d098fa25ddd8d0f340777b8c8c2b16 |
|
BLAKE2b-256 | 4edb6a567e4502e1ef44bf5ae5ec60d8c9647c408ccdf2b67c51b65d539f7f92 |