platform/packages/jinja/lib/angel3_jinja.dart
thomashii@dukefirehawk.com f2c9ab23eb Updated Jinja
2023-05-27 15:07:30 +08:00

54 lines
1.5 KiB
Dart

import 'package:angel3_framework/angel3_framework.dart';
import 'package:jinja/jinja.dart';
import 'package:jinja/loaders.dart';
/// Configures an Angel server to use Jinja2 to render templates.
///
/// By default, templates are loaded from the filesystem;
/// pass your own [createLoader] callback to override this.
///
/// All options other than [createLoader] are passed to either [FileSystemLoader]
/// or [Environment].
AngelConfigurer jinja({
Set<String> ext = const {'html'},
String path = 'lib/src/templates',
bool followLinks = true,
String blockStart = '{%',
String blockEnd = '%}',
String varOpen = '{{',
String varClose = '}}',
String commentStart = '{#',
String commentEnd = '#}',
defaultValue,
//bool autoReload = true,
Map<String, Function> filters = const <String, Function>{},
Map<String, Function> tests = const <String, Function>{},
Loader Function()? createLoader,
}) {
return (app) {
createLoader ??= () {
return FileSystemLoader(
extensions: ext,
paths: [path],
followLinks: followLinks,
);
};
var env = Environment(
loader: createLoader!(),
blockStart: blockStart,
blockEnd: blockEnd,
variableStart: varOpen,
variableEnd: varClose,
commentStart: commentStart,
commentEnd: commentEnd,
//defaultValue: defaultValue,
//autoReload: autoReload,
filters: filters,
tests: tests,
);
app.viewGenerator = (path, [values]) {
return env.getTemplate(path).render(values);
};
};
}