platform/packages/jinja/lib/angel3_jinja.dart

55 lines
1.5 KiB
Dart
Raw Normal View History

2021-06-26 10:34:23 +00:00
import 'package:angel3_framework/angel3_framework.dart';
2019-01-29 18:06:58 +00:00
import 'package:jinja/jinja.dart';
2023-05-27 07:07:30 +00:00
import 'package:jinja/loaders.dart';
2019-01-29 18:06:58 +00:00
/// 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({
2021-06-20 12:37:20 +00:00
Set<String> ext = const {'html'},
2019-01-29 18:06:58 +00:00
String path = 'lib/src/templates',
bool followLinks = true,
2021-06-20 12:37:20 +00:00
String blockStart = '{%',
String blockEnd = '%}',
2019-01-29 18:06:58 +00:00
String varOpen = '{{',
String varClose = '}}',
2021-06-20 12:37:20 +00:00
String commentStart = '{#',
String commentEnd = '#}',
2019-01-29 18:06:58 +00:00
defaultValue,
2021-06-20 12:37:20 +00:00
//bool autoReload = true,
2019-01-29 18:06:58 +00:00
Map<String, Function> filters = const <String, Function>{},
Map<String, Function> tests = const <String, Function>{},
2021-06-20 12:37:20 +00:00
Loader Function()? createLoader,
2019-01-29 18:06:58 +00:00
}) {
return (app) {
createLoader ??= () {
return FileSystemLoader(
2021-06-20 12:37:20 +00:00
extensions: ext,
2023-05-27 07:07:30 +00:00
paths: [path],
2019-01-29 18:06:58 +00:00
followLinks: followLinks,
);
};
var env = Environment(
2021-06-20 12:37:20 +00:00
loader: createLoader!(),
blockStart: blockStart,
blockEnd: blockEnd,
variableStart: varOpen,
variableEnd: varClose,
commentStart: commentStart,
commentEnd: commentEnd,
2021-02-14 05:22:25 +00:00
//defaultValue: defaultValue,
2021-06-20 12:37:20 +00:00
//autoReload: autoReload,
2019-01-29 18:06:58 +00:00
filters: filters,
tests: tests,
);
app.viewGenerator = (path, [values]) {
2023-05-27 07:07:30 +00:00
return env.getTemplate(path).render(values);
2019-01-29 18:06:58 +00:00
};
};
}