Help language development. Donate to The Perl Foundation

Hyperscript cpan:JACKMILES last updated on 2021-01-03
# hyperscript

Create hypertext with Raku.

## api

sub hyperscript(Str $tag, *%attrs, *@inner)

* `Str $tag`: 
  * The HTML tag to be created.
  * Supports class and id shortcuts, e.g.: `'p#id.class'`.
  * if `$tag` begins with a class or id shortcut, hyperscript uses a `div` element.
* `*%attrs`: HTML attributes on the element.
* `*@inner`: Inner contents for the element.

## example

use Hyperscript;
constant &h = &hyperscript;

say h('h1', 'hello, world!');
# OUTPUT: <h1>hello, world!</h1>

## slightly longer example

use Hyperscript;
constant &h = &hyperscript;

my $html =
    h('h1.classy', style => { background-color => '#22f'}, 'h')),
  h('div#menu', style => { background-color => '#2f2' },
      h('li', 'one'),
      h('li', 'two'),
      h('li', 'three'))),
  h('h2', style => { background-color => '#f22' }, 'content title'),
    "so it's just like a templating engine, ",
    "but easy to use inline with Raku"),
    "the intention is for this to be used to create ",
    "reusable, interactive html widgets."));

## 'pretty' output

By default, hyperscript outputs a condensed string of HTML. Sometimes it may be useful to have
a string representation that more closely reflects the nesting structure of your HTML. 'Pretty' output
can be enabled when needed by setting the `$*hyperscript-style` dynamic variable.

Running this Raku code:
use Hyperscript;
my &h = &hyperscript;

my @items = do for 1..3 {
  h('li.condensed', h('a.condensed', :href("page$_.html"), h('b.condensed', "Page $_")))

my $site-nav = do {
  my $*hyperscript-style = Hyperscript::Style::Pretty; # Enable pretty output
  h('nav.pretty', h('ul.pretty', @items))

say $site-nav;
Will provide the following HTML:

<nav class="pretty">
  <ul class="pretty">
    <li class="condensed"><a class="condensed" href="page1.html"><b class="condensed">Page 1</b></a></li>
    <li class="condensed"><a class="condensed" href="page2.html"><b class="condensed">Page 2</b></a></li>
    <li class="condensed"><a class="condensed" href="page3.html"><b class="condensed">Page 3</b></a></li>

## why?

* It produces HTML from Raku code, instead of a separate templating language.
* It's a way to learn Raku module development.

## prior art

* Hyperscript (JavaScript library):

## testing

prove6 -l

## license