Skip to main content
aio_pybars
========

Quick Start
------------------

0. Install::

pip install aio_pybars

OR via setup.py::

python setup.py install

1. Configure your app::

from aio_pybars import FSTemplateLoader
loop.run_until_complete(aio_pybars.setup(app,
templates_dir=config['TEMPLATES_DIR'],
Loader=FSTemplateLoader))

2. Use templates in the view::

async def index(request):
context = {"var": "value"}
return AIOBarsResponse(request, 'template_name', context)

It will render the `template_name.hbs` template with variables in the `context` to the aiohttp response.

Helpers and partials
------------------------------------

Partial is the nested template that should be included in the specific place.
If the following code occurs in the template::

{{> "sidebar"}}

pybars3 will search the _partial_ named `sidebar` in the dictionary. How to add your own partial see below.

Helper is the callable that can be called from the template. Syntactically it looks same as the variable, but can
get the arguments::

<link rel="shortcut icon" href="{{asset "favicon.ico"}}">

would call the `asset` callable with "favicon.ico" argument and put the results in the rendered template.

*To use your own partials and helpers* implement your subclass of templates loader::

class AppFSTemplateLoader(FSTemplateLoader):
def __init__(self, app, base_dir):
super().__init__(app, base_dir)

def get_partials(self):
"""
Load all files in the partials/ subdirectory of templates dir.
Method should return the dictionary {'partial_name': <compiled template>, ...}
"""
partials = super().get_partials()
base_partials = os.path.join(self.app.config['TEMPLATES_DIR'], 'partials')
for name in os.listdir(base_partials):
filename = os.path.splitext(name)[0]
template_source = open(os.path.join(base_partials, name), 'r', encoding='utf8').read()
template = self.compiler.compile(template_source)
partials[filename] = template
return partials

def get_helpers(self):
"""
Define your own set of helpers.
Method should return the dictionary {'helper_name': <callable>, ...}
"""
helpers = super().get_helpers()
helpers.update({"asset": _asset})
return helpers


def _asset(options, val, *args, **kwargs):
return "/static/{}".format(val)

and pass it as Loader argument to the setup::

loop.run_until_complete(aio_pybars.setup(app,
templates_dir=config['TEMPLATES_DIR'],
Loader=AppFSTemplateLoader))

Recursive rendering of templates
--------------------------

The aio_pybars enables templates to be recursive. If the first line of the template contains::

{{!< base_template}}

all the rendered template will be passed as variable `body` to the base template.

For example:

base.hbs::

<!DOCTYPE html>
<html>
<head>
<title>Template</title>
</head>
<body>
{{body}}
</body>

test.hbs::

{{!< base}}
Hello, {{name}}.

Then result of the `render(loader, 'test', {'name': 'Roma'})` will be::

<!DOCTYPE html>
<html>
<head>
<title>Template</title>
</head>
<body>
Hello, Roma
</body>

Release files for aio_pybars 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aio_pybars 0.1.0
File Size Uploaded
aio_pybars-0.1.0.tar.gz 3.6 kB Details

Release files / aio_pybars-0.1.0.tar.gz

Download URL aio_pybars-0.1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e0850762a9f30e713ea79a5dc9c080d8a33bd1d14fe9ebf0dd869595c0e48c9b
BLAKE2b-256 checksum
How to use checksums
14efe62432c5b998b0151629b53a0b8e6f3f71220fb960d8826c1dedb09b4bb7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page