ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - wkhere/webassembly: Web DSL for Elixir
Web DSL for Elixir. Contribute to wkhere/webassembly development by creating an account on GitHub.
Visit Site

GitHub - wkhere/webassembly: Web DSL for Elixir

GitHub - wkhere/webassembly: Web DSL for Elixir

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:

  • you produce HTML elements by using macros inside builder block
  • non-void element can be used with "flat" content argument or with a do-block
  • element with a do-block means nesting
  • inside such a do-block you have access to full Elixir syntax
  • element attributes go first (but are optional), then the content
  • attributes are Elixir keywords
  • underscores in attribute keys are translated to dash signs
  • you can omit brackets around attributes when using do-block, but not when using flat form
  • void HTML elements correspond to macros with attributes only, like meta above
  • if you want to emit just text without surrounding html tags, simply use text macro.

For me it's beautiful. What about you?

Why?

  • to have views in pure Elixir, without HTML templates
  • to utilize Erlang's approach: you can feed sockets with iolists instead of one big binary produced by template engine

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.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory