Expand description
html
hot male
hotman
.
🥵 Simple HTML generation in pure Rust with no macros 🥵
Usage
Writing HTML with hotman
is very similar to writing HTML itself.
All the same words are there, only the punctuation is different.
Elements
Html elements are constructed using functions with the same name as the tag.
Examples are head
, body
, div
, and p
.
Elements can be converted to strings with the Display
trait (and by extension, the ToString::to_string
method).
ElementData
The ElementData
trait is implemented for any type which adds either attributes or children to an element.
ElementData
is also implemented for Option
s, arrays, Vec
s, some iterators, and tuples of ElementData
s up to 20.
The element functions all take an ElementData
as their argument, so you can pass tuples for multiple values.
Attributes
Attributes are represented by structs with the same name as the attribute. They implement ElementData
.
Examples are Id
, Href
, Class
, and Style
.
Events
Individual event handler attributes do not each have their own struct.
Instead, they can be added to elements via the On
struct.
On
implements ElementData
and consists of an Event
and a string representing the handler.
Static Example
use hotman::*;
let dom = html((
Comment("A simple login page"),
head((
meta(Charset("utf-8")),
title("Login"),
script(Src("/script.js")),
)),
body((
h1("Login"),
form((
(Action("/login"), Method("POST")),
input((
Type("text"),
Name("username"),
Placeholder("Username"),
On(Change, "validate_username()"),
Autofocus,
)),
input((
Type("password"),
Name("password"),
Placeholder("Password"),
On(Change, "validate_password()"),
)),
input((Type("submit"), Value("Login"))),
)),
BR,
p((
"Don't have an account? ",
a((Href("/register"), "Register")),
)),
)),
))
.page();
println!("{dom}");
Iteration
A blanket implementation of ElementData
for any Iterator
would conflict with the implementaiton for tuples.
As a workaround, ElementData
is implemented for the Map
, FilterMap
, and FlatMap
iterators.
Because you usually map data to elements anyway, these implementations are usually more than enough.
let number_list = {
use hotman::*;
ul((1..=5).map(|i| li(i.to_string())))
};
assert_eq!(number_list.to_string(), "\
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>");
Scoping
To make writing HTML as short as possible, hotman exports every element, attribute, and event in the root of the crate.
This means that there are many potential name conflicts with surrounding code.
It is recommended to scope use hotman::*
to a small block of code:
let page_head = {
use hotman::*;
head((title("My Page"), meta((Name("Description"), Content("A page")))))
};
Re-exports
pub use Event::*;
Modules
- Traits that mark elements as having attributes
- Structs that represent HTML elements
Structs
- The
accept
attribute - The
action
attribute - The
align
attribute - The
allow
attribute - The
alt
attribute - The
async
attribute - The
autocomplete
attribute - The
autofocus
attribute - The
autoplay
attribute - The
charset
attribute - The
checked
attribute - The
cite
attribute - The
class
attribute - The
clear
attribute - The
color
attribute - The
cols
attribute - The
colspan
attribute - The
command
attribute - An HTML comment
- The
content
attribute - The
controls
attribute - The
coords
attribute - The
crossorigin
attribute - The
data
attribute - The
datetime
attribute - The
decoding
attribute - The
default
attribute - The
defer
attribute - The
dir
attribute - The
dirname
attribute - The
disabled
attribute - The
download
attribute - The
enctype
attribute - The HTML events
- The
for
attribute - The
form
attribute - The
formaction
attribute - The
formenctype
attribute - The
formmethod
attribute - The
formnovalidate
attribute - The
formtarget
attribute - Wrapper around attributes that are common to all elements
- Attributes that are common to all elements
- The
headers
attribute - The
height
attribute - The
high
attribute - The
href
attribute - The
hreflang
attribute - The
http_equiv
attribute - The
icon
attribute - The
id
attribute - The
importance
attribute - The
integrity
attribute - The
intrinsicsize
attribute - The
ismap
attribute - The
itemscope
attribute - The
kind
attribute - The
label
attribute - The
list
attribute - The
loading
attribute - The
loop
attribute - The
low
attribute - The
manifest
attribute - The
max
attribute - The
max_length
attribute - The
maxlength
attribute - The
media
attribute - The
method
attribute - The
min
attribute - The
min_length
attribute - The
minlength
attribute - The
multiple
attribute - The
muted
attribute - The
name
attribute - The
nomodule
attribute - The
nonce
attribute - The
noshade
attribute - The
novalidate
attribute - Add an event handler to an element
- The
open
attribute - The
optimum
attribute - A full HTML document.
- The
pattern
attribute - The
ping
attribute - The
placeholder
attribute - The
playsinline
attribute - The
poster
attribute - The
preload
attribute - The
profile
attribute - The
radiogroup
attribute - The
readonly
attribute - The
referrerpolicy
attribute - The
rel
attribute - The
required
attribute - The
reversed
attribute - The
rows
attribute - The
rowspan
attribute - The
sandbox
attribute - The
scope
attribute - The
selected
attribute - The
shape
attribute - The
size
attribute - The
sizes
attribute - The
span
attribute - The
src
attribute - The
srcdoc
attribute - The
srclang
attribute - The
srcset
attribute - The
start
attribute - The
step
attribute - The
style
attribute - The
target
attribute - The
title
attribute - The
type
attribute - The
usemap
attribute - The
value
attribute - The
width
attribute - The
wrap
attribute - The
xmlns
attribute
Enums
- Types of event handlers
- An HTML node
Constants
- Short alias for
br(())
Traits
- Trait for types of elements
- A piece of data that can be added to an element
Functions
- Make a
<a>
element - Make a
<abbr>
element - Make a
<area>
element - Make a
<audio>
element - Make a
<b>
element - Make a
<base>
element - Make a
<bdi>
element - Make a
<bdo>
element - Make a
<blockquote>
element - Make a
<body>
element - Make a
<br>
element - Make a
<button>
element - Make a
<canvas>
element - Make a
<caption>
element - Make a
<cite>
element - Make a
<code>
element - Make a
<col>
element - Make a
<colgroup>
element - Make a
<dd>
element - Make a
<del>
element - Make a
<details>
element - Make a
<dfn>
element - Make a
<div>
element - Make a
<dl>
element - Make a
<dt>
element - Make a
<em>
element - Make a
<embed>
element - Make a
<fieldset>
element - Make a
<form>
element - Make a
<h1>
element - Make a
<h2>
element - Make a
<h3>
element - Make a
<h4>
element - Make a
<h5>
element - Make a
<h6>
element - Make a
<head>
element - Make a
<hr>
element - Make a
<html>
element - Make a
<i>
element - Make a
<iframe>
element - Make a
<img>
element - Make a
<input>
element - Make a
<ins>
element - Make a
<kbd>
element - Make a
<label>
element - Make a
<legend>
element - Make a
<li>
element - Make a
<link>
element - Make a
<map>
element - Make a
<mark>
element - Make a
<menu>
element - Make a
<menuitem>
element - Make a
<meta>
element - Make a
<meter>
element - Make a
<noscript>
element - Make a
<object>
element - Make a
<ol>
element - Make a
<option>
element - Make a
<output>
element - Make a
<p>
element - Make a
<param>
element - Make a
<progress>
element - Make a
<q>
element - Make a
<rp>
element - Make a
<rt>
element - Make a
<samp>
element - Make a
<script>
element - Make a
<select>
element - Make a
<slot>
element - Make a
<small>
element - Make a
<source>
element - Make a
<span>
element - Make a
<strong>
element - Make a
<style>
element - Make a
<sub>
element - Make a
<summary>
element - Make a
<sup>
element - Make a
<table>
element - Make a
<tbody>
element - Make a
<td>
element - Make a
<template>
element - Make a
<textarea>
element - Make a
<tfoot>
element - Make a
<th>
element - Make a
<thead>
element - Make a
<time>
element - Make a
<title>
element - Make a
<tr>
element - Make a
<track>
element - Make a
<ul>
element - Make a
<var>
element - Make a
<video>
element - Make a
<wbr>
element