nakai/html
Types
pub type Node {
Doctype(content: String)
Html(attrs: List(Attr), children: List(Node))
Head(children: List(Node))
Body(attrs: List(Attr), children: List(Node))
Fragment(children: List(Node))
Element(tag: String, attrs: List(Attr), children: List(Node))
LeafElement(tag: String, attrs: List(Attr))
Comment(content: String)
Text(content: String)
UnsafeInlineHtml(content: String)
Script(attrs: List(Attr), content: String)
Nothing
}
Constructors
-
Doctype(content: String)
Can be used anywhere in the document, and will set the doctype of the document being rendered. Usually not necessary, as documents have a default of
<!DOCTYPE html>
.Example
html.Doctype("html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"")
-
Html(attrs: List(Attr), children: List(Node))
Used for setting attributes on the root
<html>
element of the document. Children will be rendered in-place, equivalent to usinghtml.Fragment(children)
.Example
html.Html([attr.lang("en-US")], [ ... ])
-
Head(children: List(Node))
Used for placing content in the
<head>
of the document. Useful for elements like<meta>
,<title>
,<link>
, etc.Example
html.Fragment([ html.Head([ html.title("List of puppies") ]), html.div([], [ ... ]) ])
-
Body(attrs: List(Attr), children: List(Node))
Used for setting attributes on the
<body>
element of the document. Children will be rendered in-place, equivalent to usinghtml.Fragment(children)
.Example
html.Body([attr.class("dark-mode")], [ ... ])
-
Fragment(children: List(Node))
An “transparent” container that will render it’s children, but does not add anything itself to the document. If you’ve ever used
React.Fragment
or<>
and</>
in JSX/React, this is that.Example
html.ul([], [ // some puppies are hard-coded html.li_text([], "August"), // some are loaded from a server html.Fragment(puppies_fetched_from_api |> list.map(html.li_text([], _))) ]) // <ul> // <li>August</li> // <li>Dot</li> // <li>Mody</li> // <li>Spot</li> // <li>Toby</li> // </ul>
-
Element(tag: String, attrs: List(Attr), children: List(Node))
An HTML element. You shouldn’t need to reach for this very often, but it can be a handy escape hatch if there isn’t a shorthand function for the element type you need.
Example
// bad example, pls use `html.div` html.Element("div", [], [html.Text("hello, lucy!")])
-
LeafElement(tag: String, attrs: List(Attr))
An HTML element, but that does not have any children, and should be self closing. Similarly to
Element
, you shouldn’t really need this, except as an escape hatch if there isn’t a shorthand function for the element type you need.Example
// bad example, pls use `html.link` html.LeafElement("link", [attr.rel("stylesheet"), attr.href(...)])
-
Comment(content: String)
An HTML comment, which will be included in the document.
Example
html.Comment("You've uncovered my secrets!") // <!-- You've uncovered my secrets! -->
-
Text(content: String)
Some plain text to include in the document. The provided text will be escaped, to make it safe to include in the document.
Example
html.Text("hello, lucy!") // hello, lucy!
// Time to trust some unvalidated user input! :^) html.div_text([], "<script>alert('pwned');</script>") // <div><script>alert('pwned');</script></div>
-
UnsafeInlineHtml(content: String)
The dangerous cousin of
Text
. This will render the provided text as-is, without any santization. Good for things like including some HTML you just generated from a Markdown file. Bad for things like$_GET['search']
.Example
html.Text("hello, lucy!") // hello, lucy!
// Time to trust some unvalidated user input! :^) html.div([], [html.UnsafeInlineHtml("<script>alert('pwned');</script>")]) // <div><script>alert('pwned');</script></div> // Oh no, we just got got! D:
-
Script(attrs: List(Attr), content: String)
Add some JavaScript to your page! When using the document renderer, scripts will always be inserted at the end of the page, regardless of where in the document the
Script
node is, so that your content loads first. If you’re using the inline renderer, the script will just be placed as is.Nakai does not do any validation of the script content! If it contains a
</script>
, weird things will happen. It’s also, naturally, a script, with full access to everything, just like any other script, so do not use any untrusted input.Example
html.Script([], "alert('hello, lucy!')")
html.Script([attr.type_("module"), attr.src(...)], "")
-
Nothing
Renders absolutely nothing. For when you may or may not have something to render, and need a way to say “I’ve got nothing.”
Example
html.div([], [ case my_cool_feature { Enabled -> super_cool_stuff() Disabled -> html.Nothing } ])
Functions
pub fn a_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.a(attrs, children: [html.Text(text)])
pub fn abbr_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.abbr(attrs, children: [html.Text(text)])
pub fn address(attrs: List(Attr), children: List(Node)) -> Node
pub fn address_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.address(attrs, children: [html.Text(text)])
pub fn article(attrs: List(Attr), children: List(Node)) -> Node
pub fn article_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.article(attrs, children: [html.Text(text)])
pub fn aside_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.aside(attrs, children: [html.Text(text)])
pub fn audio_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.audio(attrs, children: [html.Text(text)])
pub fn b_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.b(attrs, children: [html.Text(text)])
pub fn bdi_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.bdi(attrs, children: [html.Text(text)])
pub fn bdo_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.bdo(attrs, children: [html.Text(text)])
pub fn blockquote(
attrs: List(Attr),
children: List(Node),
) -> Node
pub fn blockquote_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.blockquote(attrs, children: [html.Text(text)])
pub fn button_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.button(attrs, children: [html.Text(text)])
pub fn canvas_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.canvas(attrs, children: [html.Text(text)])
pub fn caption(attrs: List(Attr), children: List(Node)) -> Node
pub fn caption_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.caption(attrs, children: [html.Text(text)])
pub fn cite_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.cite(attrs, children: [html.Text(text)])
pub fn code_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.code(attrs, children: [html.Text(text)])
pub fn col_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.col(attrs, children: [html.Text(text)])
pub fn colgroup(attrs: List(Attr), children: List(Node)) -> Node
pub fn colgroup_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.colgroup(attrs, children: [html.Text(text)])
pub fn data_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.data(attrs, children: [html.Text(text)])
pub fn datalist(attrs: List(Attr), children: List(Node)) -> Node
pub fn datalist_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.datalist(attrs, children: [html.Text(text)])
pub fn dd_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.dd(attrs, children: [html.Text(text)])
pub fn del_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.del(attrs, children: [html.Text(text)])
pub fn details(attrs: List(Attr), children: List(Node)) -> Node
pub fn details_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.details(attrs, children: [html.Text(text)])
pub fn dfn_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.dfn(attrs, children: [html.Text(text)])
pub fn dialog_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.dialog(attrs, children: [html.Text(text)])
pub fn div_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.div(attrs, children: [html.Text(text)])
pub fn dl_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.dl(attrs, children: [html.Text(text)])
pub fn dt_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.dt(attrs, children: [html.Text(text)])
pub fn em_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.em(attrs, children: [html.Text(text)])
pub fn embed_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.embed(attrs, children: [html.Text(text)])
pub fn fieldset(attrs: List(Attr), children: List(Node)) -> Node
pub fn fieldset_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.fieldset(attrs, children: [html.Text(text)])
pub fn figcaption(
attrs: List(Attr),
children: List(Node),
) -> Node
pub fn figcaption_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.figcaption(attrs, children: [html.Text(text)])
pub fn figure_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.figure(attrs, children: [html.Text(text)])
pub fn footer_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.footer(attrs, children: [html.Text(text)])
pub fn form_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.form(attrs, children: [html.Text(text)])
pub fn h1_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h1(attrs, children: [html.Text(text)])
pub fn h2_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h2(attrs, children: [html.Text(text)])
pub fn h3_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h3(attrs, children: [html.Text(text)])
pub fn h4_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h4(attrs, children: [html.Text(text)])
pub fn h5_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h5(attrs, children: [html.Text(text)])
pub fn h6_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.h6(attrs, children: [html.Text(text)])
pub fn header_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.header(attrs, children: [html.Text(text)])
pub fn i_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.i(attrs, children: [html.Text(text)])
pub fn iframe_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.iframe(attrs, children: [html.Text(text)])
pub fn ins_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.ins(attrs, children: [html.Text(text)])
pub fn kbd_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.kbd(attrs, children: [html.Text(text)])
pub fn label_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.label(attrs, children: [html.Text(text)])
pub fn legend_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.legend(attrs, children: [html.Text(text)])
pub fn li_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.li(attrs, children: [html.Text(text)])
pub fn main_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.main(attrs, children: [html.Text(text)])
pub fn map_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.map(attrs, children: [html.Text(text)])
pub fn mark_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.mark(attrs, children: [html.Text(text)])
pub fn math_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.math(attrs, children: [html.Text(text)])
pub fn menu_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.menu(attrs, children: [html.Text(text)])
pub fn menuitem(attrs: List(Attr), children: List(Node)) -> Node
pub fn menuitem_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.menuitem(attrs, children: [html.Text(text)])
pub fn meter_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.meter(attrs, children: [html.Text(text)])
pub fn nav_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.nav(attrs, children: [html.Text(text)])
pub fn noscript(attrs: List(Attr), children: List(Node)) -> Node
pub fn noscript_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.noscript(attrs, children: [html.Text(text)])
pub fn object_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.object(attrs, children: [html.Text(text)])
pub fn ol_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.ol(attrs, children: [html.Text(text)])
pub fn optgroup(attrs: List(Attr), children: List(Node)) -> Node
pub fn optgroup_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.optgroup(attrs, children: [html.Text(text)])
pub fn option_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.option(attrs, children: [html.Text(text)])
pub fn output_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.output(attrs, children: [html.Text(text)])
pub fn p_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.p(attrs, children: [html.Text(text)])
pub fn param_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.param(attrs, children: [html.Text(text)])
pub fn picture(attrs: List(Attr), children: List(Node)) -> Node
pub fn picture_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.picture(attrs, children: [html.Text(text)])
pub fn pre_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.pre(attrs, children: [html.Text(text)])
pub fn progress(attrs: List(Attr), children: List(Node)) -> Node
pub fn progress_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.progress(attrs, children: [html.Text(text)])
pub fn q_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.q(attrs, children: [html.Text(text)])
pub fn rp_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.rp(attrs, children: [html.Text(text)])
pub fn rt_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.rt(attrs, children: [html.Text(text)])
pub fn ruby_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.ruby(attrs, children: [html.Text(text)])
pub fn s_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.s(attrs, children: [html.Text(text)])
pub fn samp_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.samp(attrs, children: [html.Text(text)])
pub fn section(attrs: List(Attr), children: List(Node)) -> Node
pub fn section_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.section(attrs, children: [html.Text(text)])
pub fn select_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.select(attrs, children: [html.Text(text)])
pub fn small_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.small(attrs, children: [html.Text(text)])
pub fn span_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.span(attrs, children: [html.Text(text)])
pub fn strong_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.strong(attrs, children: [html.Text(text)])
pub fn sub_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.sub(attrs, children: [html.Text(text)])
pub fn summary(attrs: List(Attr), children: List(Node)) -> Node
pub fn summary_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.summary(attrs, children: [html.Text(text)])
pub fn sup_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.sup(attrs, children: [html.Text(text)])
pub fn svg_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.svg(attrs, children: [html.Text(text)])
pub fn table_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.table(attrs, children: [html.Text(text)])
pub fn tbody_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.tbody(attrs, children: [html.Text(text)])
pub fn td_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.td(attrs, children: [html.Text(text)])
pub fn textarea(attrs: List(Attr), children: List(Node)) -> Node
pub fn textarea_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.textarea(attrs, children: [html.Text(text)])
pub fn tfoot_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.tfoot(attrs, children: [html.Text(text)])
pub fn th_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.th(attrs, children: [html.Text(text)])
pub fn thead_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.thead(attrs, children: [html.Text(text)])
pub fn time_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.time(attrs, children: [html.Text(text)])
pub fn tr_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.tr(attrs, children: [html.Text(text)])
pub fn u_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.u(attrs, children: [html.Text(text)])
pub fn ul_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.ul(attrs, children: [html.Text(text)])
pub fn var_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.var(attrs, children: [html.Text(text)])
pub fn video_text(attrs: List(Attr), text: String) -> Node
Shorthand for html.video(attrs, children: [html.Text(text)])