2021-06-22 09:35:08 +00:00
|
|
|
# Angel3 Html Builder
|
|
|
|
|
|
|
|
[](https://pub.dartlang.org/packages/angel3_html_builder)
|
2021-05-15 11:47:48 +00:00
|
|
|
[](https://dart.dev/null-safety)
|
|
|
|
[](https://gitter.im/angel_dart/discussion)
|
|
|
|
|
|
|
|
[](https://github.com/dukefirehawk/angel/tree/angel3/packages/html_builder/LICENSE)
|
2021-05-01 02:48:36 +00:00
|
|
|
|
|
|
|
Build HTML AST's and render them to HTML.
|
|
|
|
|
|
|
|
This can be used as an internal DSL, i.e. for a templating engine.
|
|
|
|
|
2021-06-22 09:35:08 +00:00
|
|
|
## Installation
|
|
|
|
|
2021-05-01 02:48:36 +00:00
|
|
|
In your `pubspec.yaml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
dependencies:
|
2021-05-15 11:47:48 +00:00
|
|
|
angel3_html_builder: ^2.0.0
|
2021-05-01 02:48:36 +00:00
|
|
|
```
|
|
|
|
|
2021-06-22 09:35:08 +00:00
|
|
|
## Usage
|
|
|
|
|
2021-05-01 02:48:36 +00:00
|
|
|
```dart
|
2021-05-15 11:47:48 +00:00
|
|
|
import 'package:angel3_html_builder/angel3_html_builder.dart';
|
2021-05-01 02:48:36 +00:00
|
|
|
|
2021-05-15 11:47:48 +00:00
|
|
|
void main() {
|
2021-05-01 02:48:36 +00:00
|
|
|
// Akin to React.createElement(...);
|
|
|
|
var $el = h('my-element', p: {}, c: []);
|
|
|
|
|
|
|
|
// Attributes can be plain Strings.
|
|
|
|
h('foo', p: {
|
|
|
|
'bar': 'baz'
|
|
|
|
});
|
|
|
|
|
2021-06-22 09:35:08 +00:00
|
|
|
// Null attributes do not appear.
|
2021-05-01 02:48:36 +00:00
|
|
|
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
|
|
|
|
});
|
2021-06-22 09:35:08 +00:00
|
|
|
|
2021-05-01 02:48:36 +00:00
|
|
|
// Or, a String or Map.
|
|
|
|
h('foo', p: {
|
|
|
|
'style': 'background-color: white; color: red;'
|
|
|
|
});
|
|
|
|
|
|
|
|
h('foo', p: {
|
|
|
|
'style': {
|
|
|
|
'background-color': 'white',
|
|
|
|
'color': 'red'
|
2021-06-22 09:35:08 +00:00
|
|
|
}
|
2021-05-01 02:48:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Or, a String or Iterable.
|
|
|
|
h('foo', p: {
|
|
|
|
'class': 'a b'
|
|
|
|
});
|
|
|
|
|
|
|
|
h('foo', p: {
|
|
|
|
'class': ['a', 'b']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Standard HTML5 elements:
|
|
|
|
```dart
|
2021-05-15 11:47:48 +00:00
|
|
|
import 'package:angel3_html_builder/elements.dart';
|
2021-05-01 02:48:36 +00:00
|
|
|
|
2021-05-15 11:47:48 +00:00
|
|
|
void main() {
|
2021-05-01 02:48:36 +00:00
|
|
|
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
|
2021-05-15 11:47:48 +00:00
|
|
|
String html = StringRenderer().render($dom);
|
2021-05-01 02:48:36 +00:00
|
|
|
```
|
|
|
|
|
2021-05-15 11:47:48 +00:00
|
|
|
Example with the [Angel](https://github.com/dukefirehawk/angel/tree/angel3) server-side framework,
|
|
|
|
which has [dedicated html_builder support](https://github.com/dukefirehawk/angel/tree/html):
|
2021-05-01 02:48:36 +00:00
|
|
|
|
|
|
|
```dart
|
|
|
|
import 'dart:io';
|
2021-05-15 11:47:48 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_html_builder/elements.dart';
|
2021-05-01 02:48:36 +00:00
|
|
|
|
|
|
|
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)])
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
```
|