renderJsonView method

Future<void> renderJsonView(
  1. String viewName, {
  2. Map<String, dynamic> data = const {},
})

Renders JSON data using a view template.

Implementation

Future<void> renderJsonView(
  String viewName, {
  Map<String, dynamic> data = const {},
}) async {
  final renderer = ViewRenderer.instance;
  final context = await _buildViewContext(data);
  final content = await renderer.render(viewName, context: context);

  // Try to parse as JSON, fallback to string if not valid JSON
  try {
    final jsonData = json.decode(content);
    _body.sendJson(jsonData);
  } catch (_) {
    // If not valid JSON, send as plain text
    _body.sendText(content);
  }
}