platform/packages/html/README.md

81 lines
2.5 KiB
Markdown
Raw Normal View History

2021-09-11 04:03:14 +00:00
# Angel3 HTML
2017-07-23 15:50:37 +00:00
2022-03-20 00:38:59 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_html?include_prereleases)
2021-09-11 04:03:14 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
2023-12-25 03:45:10 +00:00
[![License](https://img.shields.io/github/license/dart-backend/angel)](https://github.com/dart-backend/angel/tree/master/packages/html/LICENSE)
2021-09-11 04:03:14 +00:00
A plug-in that allows you to return `belatuk_html_builder` AST's from request handlers, and have them sent as HTML automatically.
[`package:belatuk_html_builder`](https://pub.dev/packages/belatuk_html_builder) is a simple virtual DOM library with a handy Dart DSL that makes it easy to build HTML AST's:
2017-07-23 15:50:37 +00:00
```dart
2021-09-11 04:03:14 +00:00
import 'package:belatuk_html_builder/elements.dart';
2017-07-23 15:50:37 +00:00
Node myDom = html(lang: 'en', c: [
head(c: [
meta(name: 'viewport', content: 'width=device-width, initial-scale=1'),
title(c: [
text('html_builder example page')
]),
]),
body(c: [
h1(c: [
text('Hello world!'),
]),
]),
]);
```
2022-03-20 00:38:59 +00:00
This plug-in means that you can now `return` these AST's, and Angel will automatically send them to clients. Ultimately, the implication is that you can use `belatuk_html_builder` as a substitute for a templating system within Dart. With [hot reloading](https://pub.dev/packages/angel3_hot), you won't even need to reload your server (as it should be).
2017-07-23 15:50:37 +00:00
2021-09-11 04:03:14 +00:00
## Installation
2017-07-23 15:50:37 +00:00
In your `pubspec.yaml`:
```yaml
dependencies:
2022-04-23 04:21:39 +00:00
angel3_html: ^6.0.0
2017-07-23 15:50:37 +00:00
```
2021-09-11 04:03:14 +00:00
## Usage
2017-07-23 15:50:37 +00:00
The `renderHtml` function does all the magic for you.
```dart
configureServer(Angel app) async {
// Wire it up!
2019-01-06 18:11:11 +00:00
app.fallback(renderHtml());
2017-07-23 15:50:37 +00:00
// You can pass a custom StringRenderer if you need more control over the output.
2019-01-06 18:11:11 +00:00
app.fallback(renderHtml(renderer: new StringRenderer(html5: false)));
2017-07-23 15:50:37 +00:00
app.get('/greet/:name', (RequestContext req) {
return html(lang: 'en', c: [
head(c: [
meta(name: 'viewport', content: 'width=device-width, initial-scale=1'),
title(c: [
text('Greetings!')
]),
]),
body(c: [
h1(c: [
text('Hello, ${req.params['id']}!'),
]),
]),
]);
});
}
```
2022-03-20 00:38:59 +00:00
By default, `renderHtml` will ignore the client's `Accept` header. However, if you pass `enforceAcceptHeader` as `true`, then a `406 Not Acceptable` error will be thrown if the client doesn't accept `*/*` or `text/html`.
2017-07-23 15:50:37 +00:00
```dart
configureServer(Angel app) async {
// Wire it up!
2019-01-06 18:11:11 +00:00
app.fallback(renderHtml(enforceAcceptHeader: true));
2017-07-23 15:50:37 +00:00
// ...
}
2021-09-11 04:03:14 +00:00
```