view method

Future<Response> view(
  1. String template, {
  2. Map<String, dynamic> data = const {},
  3. int statusCode = HttpStatus.ok,
})

Renders template and writes the result as an HTML response.

The manager selects an engine from the extension in template, passes data to that engine, and uses statusCode for the response. This helper renders in-memory content through ViewEngineManager.render; use RoutedViewContext.template for a file-backed template.

The request container must contain a ViewEngineManager. If no engine matches the template extension, or if the engine fails, the exception is propagated and the response is not written by this helper.

Implementation

Future<Response> view(
  String template, {
  Map<String, dynamic> data = const {},
  int statusCode = HttpStatus.ok,
}) async {
  final manager = container.get<ViewEngineManager>();
  final content = await manager.render(template, data);
  response.statusCode = statusCode;
  // Set the value through the portable header API. Assigning
  // dart:io's ContentType.html is not supported by every Fetch runtime
  // (notably Cloudflare Workers), while the wire representation is the
  // same on native and portable hosts.
  response.headers.set('Content-Type', 'text/html; charset=utf-8');
  response.write(content);
  return response;
}