renderToDocument function

String renderToDocument(
  1. BloomNode body, {
  2. String lang = 'en',
  3. String charset = 'UTF-8',
  4. String? title,
  5. List<BloomNode> head = const [],
  6. String? importMapJson,
  7. List<String> stylesheets = const [],
  8. List<String> scripts = const [],
})

Renders a complete HTML5 document wrapping body with standard boilerplate.

Produces a complete <!DOCTYPE html><html lang="...">...</html> string with metadata tags, stylesheets, custom head elements, body content, and scripts.

  • body: The main content BloomNode placed inside the <body> element.
  • lang: Value for the <html lang="..."> attribute. Defaults to 'en'.
  • charset: Charset meta tag value (<meta charset="...">). Defaults to 'UTF-8'.
  • title: Page title placed in <title>. Omitted if null.
  • head: Additional BloomNode descriptors rendered directly inside <head>.
  • importMapJson: Optional JSON string emitted inside <script type="importmap"> in <head>.
  • stylesheets: List of CSS stylesheet URLs emitted as <link rel="stylesheet"> tags in <head>.
  • scripts: List of JavaScript URLs emitted as <script src="..."> tags at the bottom of <body>.
final html = renderToDocument(
  Div(className: 'app', text: 'Welcome to Bloom'),
  title: 'Bloom App',
  stylesheets: ['/styles/app.css'],
  scripts: ['/main.dart.js'],
);

Implementation

String renderToDocument(
  BloomNode body, {
  String lang = 'en',
  String charset = 'UTF-8',
  String? title,
  List<BloomNode> head = const [],
  String? importMapJson,
  List<String> stylesheets = const [],
  List<String> scripts = const [],
}) {
  return runZoned(() {
    final buf = StringBuffer();
    buf.write('<!DOCTYPE html>\n<html lang="${escapeHtml(lang)}">\n<head>\n');
    buf.write('<meta charset="${escapeHtml(charset)}">\n');
    buf.write(
        '<meta name="viewport" content="width=device-width, initial-scale=1">\n');
    if (title != null) {
      buf.write('<title>${escapeHtml(title)}</title>\n');
    }
    if (importMapJson != null) {
      buf.write('<script type="importmap">$importMapJson</script>\n');
    }
    for (final url in stylesheets) {
      buf.write('<link rel="stylesheet" href="${escapeHtml(url)}">\n');
    }
    for (final node in head) {
      _render(node, buf);
      buf.write('\n');
    }
    buf.write('</head>\n<body>\n');
    _render(body, buf);
    buf.write('\n');
    for (final url in scripts) {
      buf.write('<script src="${escapeHtml(url)}"></script>\n');
    }
    buf.write('</body>\n</html>');
    return buf.toString();
  }, zoneValues: {_keyframesZoneKey: <String>{}});
}