hyper 1.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 50

hyper #

Hyper makes it easy to build HTML using Dart. It's intentionally kept simple, and produces nothing but strings of HTML.

Usage #

import 'package:hyper/hyper.dart';

main() {
  // Create elements using `hyper`, a tag name, attributes, and a list of
  // child elements.
  var header = hyper(
    'h1',
    attrs: {'class': 'header'},
    children: [
      // `t` produces a special element that does not use a tag. Use it for
      // text nodes.
      t('Hello, world!'),
    ],
  );
  print(header); // <h1 class="header">Hello, world!</h1>
}

Hyper Elements #

hyper also has an optional export that includes all of the HTML tags as individual functions. This makes it a little easier to ensure that you're not doing something silly like passing a child element to an img tag.

import 'package:hyper/hyper.dart';
import 'package:hyper/elements.dart' as h;

main() {
  var header = h.h1(
    attrs: {
      'class': 'header',
    },
    children: [
      t('Hello, world!'),
    ],
  );
  print(header); // <h1 class="header">Hello, world!</h1>

  // This won't even compile!
  // var invalidElement = h.img(children: [t('Not gonna work!')]);
}

Hyper Strict Elements #

Hyper's Strict Elements are a variant of Elements that include keyword arguments for all available HTML attributes for a given element.

import 'package:hyper/hyper.dart';
import 'package:hyper/strict_elements.dart' as h;

main() {
  var link = h.a(
    href: 'https://google.com',
    children: [
      t('Hello, world!'),
    ],
  );
  print(link); // <a href="https://google.com">Hello, world!</h1>

  // You can still use `attrs` to pass in custom HTML attributes.
  var header = h.h1(
    attrs: {
      'role': 'heading',
      'aria-level': '1',
    },
    children: [
      t('Hello, world!'),
    ],
  );
  print(header); // <h1 role="heading" aria-level="1">Hello, world!</h1>
}

What this package isn't #

hyper is a tool to make it easier to generate static HTML. It doesn't provide anything exciting like virtual DOM or event listeners. It's simple, and just generates a text HTML string.

Prior Art #

hyper was heavily inspired by HyperScript.

1.1.0 #

  • Added Strict Elements

1.0.1 #

  • Minor formatting updates to appease pub.

1.0.0 #

  • Initial version, created by Stagehand

example/hyper_example.dart

import 'package:hyper/hyper.dart';
import 'package:hyper/elements.dart' as h;
import 'package:hyper/strict_elements.dart' as el;

main() {
  // Hyper can be used to compose full HTML documents.
  var fullHtmlDoc = hyper('html', children: [
    hyper('head', children: [
      hyper('title', children: [
        t('Hello, world!'),
      ])
    ]),
    hyper('body', children: [
      hyper(
        'h1',
        attrs: {'class': "greeting"},
        children: [
          t('Hello from Hyper!'),
        ],
      )
    ]),
  ]);
  print(fullHtmlDoc);
  // <html>
  //   <head>
  //     <title>Hello, world!</title>
  //   </head>
  //   <body>
  //     <h1 class="greeting">Hello from Hyper!</h1>
  //   </body>
  // </html>

  // You can also use the Elements package to make things a bit easier.
  var header = h.header(
    attrs: {'class': 'heading'},
    children: [
      h.h1(children: [
        // Text nodes can be created using t.
        t('Title'),
      ])
    ],
  );
  print(header);
  // <header class="heading">
  //   <h1>Title</h1>
  // </header>

  // Using the Elements package also makes sure you don't accidentally pass
  // children to a void element. This won't even compile!
  // var invalidElement = h.img(children: [t('Not gonna work!')]);

  // If you want even stricter elements, you can use StrictElements!
  var strictHeader = el.header(
    class_: 'heading',
    children: [
      el.h1(children: [t('Title')]),
    ],
  );
  print(strictHeader);
  // <header class="heading">
  //   <h1>Title</h1>
  // </header>

  // You can also compose Elements together.
  Element container({List<Element> children}) {
    return h.div(attrs: {'class': 'container'}, children: children);
  }

  Element userProfile(String username, String petsName) {
    return h.div(
      attrs: {'class': 'userProfile'},
      children: [
        h.h2(
          attrs: {'class': 'username'},
          children: [
            t(username),
          ],
        ),
        h.p(
          attrs: {'class': 'petsName'},
          children: [
            t("Parent to: $petsName"),
          ],
        ),
      ],
    );
  }

  var userProfiles = container(
    children: [
      userProfile('dstaley', 'Jpeg'),
      userProfile('paddycarver', 'Roxy'),
    ],
  );
  print(userProfiles);
  // <div class="container">
  //   <div class="userProfile">
  //     <h2 class="username">dstaley</h2>
  //     <p class="petsName">Parent to: Jpeg</p>
  //   </div>
  //   <div class="userProfile">
  //     <h2 class="username">paddycarver</h2>
  //     <p class="petsName">Parent to: Roxy</p>
  //   </div>
  // </div>

  var heading = h.h1(
    attrs: {
      'role': 'heading',
      'aria-level': '1',
    },
    children: [
      t('Hello, world!'),
    ],
  );
  print(heading);
  // <h1 role="heading" aria-level="1">Hello, world!</h1>
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  hyper: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:hyper/hyper.dart';
  
Version Uploaded Documentation Archive
1.1.0 Jan 3, 2019 Go to the documentation of hyper 1.1.0 Download hyper 1.1.0 archive
1.0.1 Dec 17, 2018 Go to the documentation of hyper 1.0.1 Download hyper 1.0.1 archive
1.0.0 Dec 17, 2018 Go to the documentation of hyper 1.0.0 Download hyper 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jun 26, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:hyper/hyper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
build ^1.0.2
build_runner ^1.1.2
code_builder ^3.1.3
dart_style ^1.2.2
source_gen ^0.9.3
test ^1.0.0