Rendering Views

Rendering Views

Just like res.render in Express, Angel's ResponseContext exposes a Future called render. This invokes whichever function is assigned to your server's viewGenerator.
There is a Mustache templating plug-in for Angel available: https://github.com/dukefirehawk/angel/tree/master/packages/mustache​
There is also Jael, one of the few actively-developed HTML templating engines for Dart.
Angel support for Jael is provided through package:angel_jael.
Another is Jinja2, which was recently ported by to Dart by Olzhas Suleimen.
Angel support for Jinja2 can be found here: https://github.com/dukefirehawk/angel/tree/master/packages/angel_jinja​

Example

1
app.get('/view', (req, res) async => await res.render('hello', {'locals': ['foo', 'bar']});
Copied!

ViewGenerator

Angel declares the following typedef:
1
/// A function that asynchronously generates a view from the given path and data.
2
typedef Future<String> ViewGenerator(String path, [Map data]);
Copied!
A templating plug-in can assign one of these to app.viewGenerator to set itself up:
1
import 'dart:io';
2
import 'package:angel3_framework/angel3_framework.dart';
3
​
4
Future<void> plugin(Angel app) async {
5
app.viewGenerator = (String path, [Map data]) async {
6
return "Requested view $path with locals: $data";
7
};
8
}
9
​
10
void main() async {
11
var app = Angel();
12
await app.configure(plugin);
13
await app.startServer();
14
}
Copied!

Next Up...

    1.
    Explore Angel's isomorphic client library.
    2.
    Find out how to test Angel applications.
Last modified 4mo ago