renderFragments method
Render multiple named fragments from a template string, concatenated in order.
Designed for HTMX OOB (out-of-band) swap responses. Each fragment is processed independently with the same context. Fails fast if any fragment is missing — no partial output is returned.
Implementation
String renderFragments(String source, {required List<String> fragments, required Map<String, dynamic> context}) {
if (fragments.isEmpty) return '';
var doc = _parse(source);
doc = _resolveInheritance(doc);
final attrPrefix = '$prefix$separator';
final escapedPrefix = escapeAttrSelector(attrPrefix);
// Fail-fast: find and clone all fragment elements before processing any
final clones = <Element>[];
for (final name in fragments) {
final el = doc.querySelector('[${escapedPrefix}fragment="$name"]');
if (el == null) throw FragmentNotFoundException(name);
clones.add(el.clone(true));
}
// Process each clone independently
final processor = _createProcessor()..collectFragments(doc);
final buffer = StringBuffer();
for (final clone in clones) {
processor.process(clone, context);
clone.attributes.remove('${attrPrefix}fragment');
buffer.write(_elementHtml(clone, attrPrefix));
}
return buffer.toString();
}