platform/packages/jael/angel_jael/README.md

100 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

# Protevus Jael
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_jael?include_prereleases)
2021-05-15 10:45:39 +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)
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/jael/angel_jael/LICENSE)
2017-10-01 03:14:44 +00:00
[Protevus 3](https://pub.dev/packages/protevus_framework) support for [Jael 3](https://pub.dev/packages/jael3).
2021-05-15 10:45:39 +00:00
## Installation
2017-10-01 03:14:44 +00:00
In your `pubspec.yaml`:
```yaml
dependencies:
protevus_jael: ^8.0.0
2017-10-01 03:14:44 +00:00
```
## Usage
Just like `mustache` and other renderers, configuring Protevus to use Jael is as simple as calling `app.configure`:
2017-10-01 03:14:44 +00:00
```dart
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_jael/protevus_jael.dart';
2017-10-01 03:14:44 +00:00
import 'package:file/file.dart';
ProtevusConfigurer myPlugin(FileSystem fileSystem) {
return (Protevus app) async {
2017-10-01 03:14:44 +00:00
// Connect Jael to your server...
await app.configure(
jael(fileSystem.directory('views')),
);
};
}
```
`package:protevus_jael` supports caching views and minified html output by default, to improve performance. You might want to disable them in development, so consider setting these flags to `false`:
2017-10-01 03:14:44 +00:00
```dart
2021-12-22 23:34:41 +00:00
jael(viewsDirectory, cacheViews: false, minified: false);
2017-10-01 03:14:44 +00:00
```
Keep in mind that this package uses `package:file`, rather than `dart:io`.
2017-10-01 03:14:44 +00:00
The following is a basic example of a server setup that can render Jael templates from a directory named `views`:
2017-10-01 03:14:44 +00:00
```dart
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_jael/protevus_jael.dart';
2017-10-01 03:14:44 +00:00
import 'package:file/local.dart';
import 'package:logging/logging.dart';
void main() async {
var app = Protevus();
2017-10-01 03:14:44 +00:00
var fileSystem = const LocalFileSystem();
await app.configure(
jael(fileSystem.directory('views')),
);
2018-11-10 21:48:03 +00:00
// Render the contents of views/index.jael
2017-10-01 03:14:44 +00:00
app.get('/', (res) => res.render('index', {'title': 'ESKETTIT'}));
app.use(() => throw ProtevusHttpException.notFound());
2017-10-01 03:14:44 +00:00
app.logger = Logger('protevus')
2017-10-01 03:14:44 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
var server = await app.startServer(null, 3000);
print('Listening at http://${server.address.address}:${server.port}');
}
2017-10-02 16:12:51 +00:00
```
2017-10-01 03:14:44 +00:00
To apply additional transforms to parsed documents, provide a set of `patch` functions, like in `package:jael3_preprocessor`.
2021-12-25 02:40:30 +00:00
## Performance Optimization
For handling large volume of initial requests, consider using `jaelTemplatePreload` to preload all the JAEL templates
into an external cache.
```dart
var templateDir = fileSystem.directory('views');
// Preload JAEL view templates into cache
var viewCache = <String, Document>{};
jaelTemplatePreload(templateDir, viewCache);
// Inject cache into JAEL renderer
await app.configure(
jael(fileSystem.directory('views'), cache: viewCache),
);
```