You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DSL for creating html structure straight with Elixir blocks:
useWebAssemblybuilderdohtmldoheaddometahttp_equiv: "Content-Type",content: "text/html"title"example"endbodydodivclass: "container",id: :contentdouldoforindex<-1..3,do:li"item #{index}"endrandom=:random.uniform(10)ifrandom==5dotext"Lucky! You got five"endendspan[style: "smiling"],"that was nice"endendend
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:
defmodulePluggeddoimportPlug.ConnusePlug.RouteruseWebAssemblyplug:matchplug:dispatchget"/"dodoc=builderdohtmldobodydotext"hello from Plug!"endendendconn|>put_resp_content_type("text/html")|>send_resp(200,doc)endend