https://github.com/dongweiming/sanic-mako
Mako support for sanic
https://github.com/dongweiming/sanic-mako
Last synced: 3 months ago
JSON representation
Mako support for sanic
- Host: GitHub
- URL: https://github.com/dongweiming/sanic-mako
- Owner: dongweiming
- License: apache-2.0
- Created: 2018-12-09T09:56:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-19T07:47:22.000Z (over 3 years ago)
- Last Synced: 2025-03-17T14:01:55.614Z (3 months ago)
- Language: Python
- Size: 17.6 KB
- Stars: 17
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sanic-mako
Mako support for sanic## Installation
`python3 -m pip install sanic-mako`
## Features
`sanic-mako` supports:
- `@mako.template` syntax
- use `render_template_def` render a specific def from a given template
- use `render_template` render a template from the template folder with the given context
- factory pattern `init_app` method for creating apps## Usage
```python
from sanic import Sanic
from sanic.response import json
from sanic_mako import SanicMakoapp = Sanic()
mako = SanicMako(app)
# or setup later
# mako = SanicMako()
# mako.init_app(app)@app.route('/')
@mako.template('index.html') # decorator method is staticmethod
async def index(request):
return {'greetings': 'Hello, sanic!'}
@bp.route('/login', methods=['GET', 'POST'])
async def login(request):
error = None
return await render_template('admin/login_user.html', request,
{'error': error})
@bp.route('/post//react', methods=['POST', 'DELETE'])
async def react(request, post_id):
# ...
return json({ 'r': 0, 'html': await render_template_def(
'utils.html', 'render_react_container', request,
{'reaction_type': reaction_type }) })if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)
```