platform-common-utilities/packages/html_builder
thomashii@dukefirehawk.com 0aee2f64da Updated lints
2023-11-16 23:44:09 +08:00
..
example Added html_builder package 2021-09-11 10:17:30 +08:00
lib Migrated to Dart 3 2023-03-26 09:34:19 +08:00
test Migrated to Dart 3 2023-03-26 09:34:19 +08:00
analysis_options.yaml Added html_builder package 2021-09-11 10:17:30 +08:00
AUTHORS.md Added html_builder package 2021-09-11 10:17:30 +08:00
CHANGELOG.md Updated to support Dart 3 2023-05-16 00:04:04 +08:00
LICENSE Added html_builder package 2021-09-11 10:17:30 +08:00
melos_belatuk_html_builder.iml Upgraded to support sdk 2.17 2022-07-06 22:04:09 +08:00
pubspec.yaml Updated lints 2023-11-16 23:44:09 +08:00
README.md Updated lints 2023-11-16 23:44:09 +08:00

Betaluk Html Builder

Pub Version (including pre-releases) Null Safety License

Replacement of package:html_builder with breaking changes to support NNBD.

This package builds HTML AST's and renders them to HTML. It can be used as an internal DSL, i.e. for a templating engine.

Requirements

  • Dart SDK: 3.0.x or later

Installation

In your pubspec.yaml:

dependencies:
  belatuk_html_builder: ^8.0.0

Usage

import 'package:belatuk_html_builder/belatuk_html_builder.dart';

void main() {
    // Akin to React.createElement(...);
    var $el = h('my-element', p: {}, c: []);

    // Attributes can be plain Strings.
    h('foo', p: {
        'bar': 'baz'
    });

    // Null attributes do not appear.
    h('foo', p: {
        'does-not-appear': null
    });

    // If an attribute is a bool, then it will only appear if its value is true.
    h('foo', p: {
        'appears': true,
        'does-not-appear': false
    });

    // Or, a String or Map.
    h('foo', p: {
        'style': 'background-color: white; color: red;'
    });

    h('foo', p: {
        'style': {
            'background-color': 'white',
            'color': 'red'
        }
    });

    // Or, a String or Iterable.
    h('foo', p: {
        'class': 'a b'
    });

    h('foo', p: {
        'class': ['a', 'b']
    });
}

Standard HTML5 elements:

import 'package:belatuk_html_builder/elements.dart';

void main() {
    var $dom = html(lang: 'en', c: [
        head(c: [
            title(c: [text('Hello, world!')])
        ]),
        body(c: [
            h1(c: [text('Hello, world!')]),
            p(c: [text('Ok')])
        ])
    ]);
}

Rendering to HTML:

String html = StringRenderer().render($dom);

Example implementation with the Angel3 backend framework, which uses dedicated html_builder package:

import 'dart:io';
import 'package:belatuk_framework/belatuk_framework.dart';
import 'package:belatuk_html_builder/elements.dart';

configureViews(Angel app) async {
    app.get('/foo/:id', (req, res) async {
        var foo = await app.service('foo').read(req.params['id']);
        return html(c: [
            head(c: [
                title(c: [text(foo.name)])
            ]),
            body(c: [
                h1(c: [text(foo.name)])
            ])
        ]);
    });
}