platform/packages/html/README.md

81 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

# Protevus HTML
2017-07-23 15:50:37 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/protevus_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)
2024-07-07 15:02:49 +00:00
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/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!'),
]),
]),
]);
```
This plug-in means that you can now `return` these AST's, and Protevus 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/protevus_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:
protevus_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(Protevus app) async {
2017-07-23 15:50:37 +00:00
// 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(Protevus app) async {
2017-07-23 15:50:37 +00:00
// 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
```