wkhere / webassembly

Web DSL for Elixir
BSD 2-Clause "Simplified" License
73 stars 9 forks source link

WebAssembly

beauty inside! Build Status Coverage Status hex.pm version hex.pm downloads

DSL for creating html structure straight with Elixir blocks:

    use WebAssembly
    builder do
      html do
        head do
          meta http_equiv: "Content-Type", content: "text/html"
          title "example"
        end
        body do
          div class: "container", id: :content do
            ul do
              for index <- 1..3, do:
                li "item #{index}"
            end
            random = :random.uniform(10)
            if random == 5 do
              text "Lucky! You got five"
            end
          end
          span [style: "smiling"], "that was nice"
        end
      end
    end

This results in a deeply nested list (aka iolist) which you can flatten or (better!) send to the socket as it is (via Plug & Cowboy for example).

Now what can be concluded from the example above:

For me it's beautiful. What about you?

Why?

You can possibly mix different styles: code small snippets in WebAssembly and feed them to your partial templates, finally using your template engine to render the whole page.

Usage

WebAssembly is published on Hex, so just add {:webassembly, "~> 0.6"} to your deps and :webassembly to your apps in the mix.exs.

Using it with Plug is a no-brainer - you just pass the doc to send_resp/3:

defmodule Plugged do
  import Plug.Conn
  use Plug.Router
  use WebAssembly

  plug :match
  plug :dispatch

  get "/" do
    doc = builder do
      html do
        body do
          text "hello from Plug!"
        end
      end
    end
    conn
    |> put_resp_content_type("text/html")
    |> send_resp(200, doc)
  end
end

API details are available at hexdocs.

TDD

WebAssembly aims to have 100% test coverage.

Type Safety

As for releases 0.3.0 and above WebAssembly dialyzes with no warnings.

Thanks

Loosely inspired by Markaby.

License

The code is released under the BSD 2-Clause License.