html

Pubbuild status

A plug-in that allows you to return html_builder AST's from request handlers, and have them sent as HTML automatically.

package:html_builder is a simple virtual DOM library(without diffing, you can find thathere), with a handy Dart DSL that makes it easy to build HTMLAST's:

  1. import 'package:html_builder/elements.dart';
  2.  
  3. Node myDom = html(lang: 'en', c: [
  4. head(c: [
  5. meta(name: 'viewport', content: 'width=device-width, initial-scale=1'),
  6. title(c: [
  7. text('html_builder example page')
  8. ]),
  9. ]),
  10. body(c: [
  11. h1(c: [
  12. text('Hello world!'),
  13. ]),
  14. ]),
  15. ]);

This plug-in means that you can now return these AST's, and Angel will automatically send them toclients. Ultimately, the implication is that you can use html_builder as a substitute for atemplating system within Dart. With hot reloading, you won'teven need to reload your server (as it should be).

Installation

In your pubspec.yaml:

  1. dependencies:
  2. angel_html: ^1.0.0

Usage

The renderHtml function does all the magic for you.

  1. configureServer(Angel app) async {
  2. // Wire it up!
  3. app.fallback(renderHtml());
  4.  
  5. // You can pass a custom StringRenderer if you need more control over the output.
  6. app.fallback(renderHtml(renderer: new StringRenderer(html5: false)));
  7.  
  8. app.get('/greet/:name', (RequestContext req) {
  9. return html(lang: 'en', c: [
  10. head(c: [
  11. meta(name: 'viewport', content: 'width=device-width, initial-scale=1'),
  12. title(c: [
  13. text('Greetings!')
  14. ]),
  15. ]),
  16. body(c: [
  17. h1(c: [
  18. text('Hello, ${req.params['id']}!'),
  19. ]),
  20. ]),
  21. ]);
  22. });
  23. }

By default, renderHtml will ignore the client's Accept header. However, if you passenforceAcceptHeader as true, then a 406 Not Acceptable error will be thrown if theclient doesn't accept / or text/html.

  1. configureServer(Angel app) async {
  2. // Wire it up!
  3. app.fallback(renderHtml(enforceAcceptHeader: true));
  4.  
  5. // ...
  6. }