brackets

A lightweight Dart package for building and rendering markup (HTML/XML).

Table of Contents

Overview

brackets offers a concise way to create markup structures in Dart using a unique approach based on Dart's extension methods and operators. Instead of providing a fluent API, it leverages extensions to create a terse, yet powerful markup generation system.

The resulting structure is an Abstract Syntax Tree (AST), which is represented using Algebraic Data Types (ADTs). This allows for easy manipulation and transformation of the markup structure.

Installation

Add to your pubspec.yaml:

dependencies:
  brackets: ^1.0.0

Usage

Basic Markup

Create simple markup nodes:

final node = 'div'(['Hello'.$]);
print(node.render());

Output:

<div>Hello</div>

Working with HTML

Build complete HTML documents:

final document = html([
  'head'([
    'title'(['My Page'.$]),
  ]),
  'body'([
    'h1'(['Welcome'.$]),
    'p'(['Content here'.$]),
  ]),
]);

print(document.render());

Output:

<!DOCTYPE html><html><head><title>My Page</title></head><body><h1>Welcome</h1><p>Content here</p></body></html>

Working with XML

Create XML documents:

final xmlDoc = xml([
  'root'([
    'child'(['data'.$]),
  ]),
]);

print(xmlDoc.render());

Output:

<?xml version="1.0" encoding="UTF-8"?><root><child>data</child></root>

Attributes

Add attributes to tags:

final element = 'div'(
  ['Content'.$],
  attrs: {'class': 'container', 'id': 'main'},
);

print(element.render());

Output:

<div class="container" id="main">Content</div>

For more examples, check the example file.

Libraries

brackets