platform-common-utilities/packages/html_builder/README.md

118 lines
2.7 KiB
Markdown
Raw Normal View History

2021-09-11 02:17:30 +00:00
# Betaluk Html Builder
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/belatuk_html_builder?include_prereleases)
2021-09-11 02:17:30 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
2021-09-12 03:21:17 +00:00
[![License](https://img.shields.io/github/license/dart-backend/belatuk-common-utilities)](https://github.com/dart-backend/belatuk-common-utilities/blob/main/packages/html_builder/LICENSE)
2021-09-11 02:17:30 +00:00
**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.
2021-09-11 02:47:03 +00:00
## Requirements
2023-05-15 16:04:04 +00:00
* Dart SDK: 3.0.x or later
2021-09-11 02:47:03 +00:00
2021-09-11 02:17:30 +00:00
## Installation
In your `pubspec.yaml`:
```yaml
dependencies:
2023-03-26 01:34:19 +00:00
belatuk_html_builder: ^5.0.0
2021-09-11 02:17:30 +00:00
```
## Usage
```dart
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:
```dart
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:
```dart
String html = StringRenderer().render($dom);
```
2021-09-11 02:41:31 +00:00
Example implementation with the [Angel3](https://pub.dev/packages/angel3_framework) backend framework,
which uses [dedicated html_builder package](https://github.com/dukefirehawk/angel/tree/html):
2021-09-11 02:17:30 +00:00
```dart
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)])
])
]);
});
}
```