renderTemplate method

Future<void> renderTemplate(
  1. HttpRequest request,
  2. String templatePath,
  3. Map<String, dynamic> context
)

Renders a template file with the given context.

The template file should be an HTML file with placeholders in the form of {{ key }} where key is a key in the context map.

The rendered template is written to the response and the response is closed.

If the template file does not exist or cannot be rendered, a 500 error is sent to the client and the request is not closed.

  • request: The HTTP request.
  • templatePath: The path to the template file.
  • context: The map containing the context for the template.

Implementation

Future<void> renderTemplate(HttpRequest request, String templatePath,
    Map<String, dynamic> context) async {
  try {
    String renderedTemplate =
        await _templateEngine!.render(templatePath, context);
    request.response
      ..headers.contentType = ContentType.html
      ..write(renderedTemplate)
      ..close();
  } catch (e) {
    _handle500(request, e);
  }
}