2024-10-12 10:35:14 +00:00
|
|
|
# Jinja View Template for Protevus
|
2019-01-29 18:06:58 +00:00
|
|
|
|
2024-10-13 01:45:27 +00:00
|
|
|
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_jinja?include_prereleases)
|
2021-06-26 10:34:23 +00:00
|
|
|
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
2024-07-07 15:02:49 +00:00
|
|
|
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
|
2024-10-12 10:35:14 +00:00
|
|
|
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/jinja/LICENSE)
|
2021-06-26 10:34:23 +00:00
|
|
|
|
2024-10-12 10:41:18 +00:00
|
|
|
A service that renders Jinja2 view template into HTML for [Protevus](https://protevus-framework.web.app) framework. Ported from Python to Dart.
|
2021-06-26 10:34:23 +00:00
|
|
|
|
|
|
|
## Example
|
2019-01-29 18:06:58 +00:00
|
|
|
|
|
|
|
```dart
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_framework/http.dart';
|
|
|
|
import 'package:angel_jinja/angel_jinja.dart';
|
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
|
2021-06-20 12:37:20 +00:00
|
|
|
void main() async {
|
2024-10-12 10:39:20 +00:00
|
|
|
var app = Protevus();
|
2024-10-13 02:17:24 +00:00
|
|
|
var http = ProtevusHttp(app);
|
2019-01-29 18:06:58 +00:00
|
|
|
var viewsDir = p.join(
|
|
|
|
p.dirname(
|
|
|
|
p.fromUri(Platform.script),
|
|
|
|
),
|
|
|
|
'views',
|
|
|
|
);
|
|
|
|
|
|
|
|
// Enable Jinja2 views
|
|
|
|
await app.configure(jinja(path: viewsDir));
|
|
|
|
|
|
|
|
// Add routes.
|
|
|
|
// See: https://github.com/ykmnkmi/jinja.dart/blob/master/example/bin/server.dart
|
|
|
|
|
|
|
|
app
|
|
|
|
..get('/', (req, res) => res.render('index.html'))
|
|
|
|
..get('/hello', (req, res) => res.render('hello.html', {'name': 'user'}))
|
|
|
|
..get('/hello/:name', (req, res) => res.render('hello.html', req.params));
|
|
|
|
|
|
|
|
app.fallback((req, res) {
|
|
|
|
res
|
|
|
|
..statusCode = 404
|
|
|
|
..write('404 Not Found :(');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
await http.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at ${http.uri}');
|
|
|
|
}
|
2021-06-26 10:34:23 +00:00
|
|
|
```
|