htmxpress
Procedural macros for quickly generating htmx from rust structs.
Attributes
Reference:
element
Include the field contents in the final HTML inside the specified element.
Only one element attribute is allowed per field/struct.
When applied on structs, all elements inside it will be wrapped in the specified element.
Fields that do not have this attribute will be ignored when generating the HTML. Subsequently, any other attributes related to the HTML element will also be ignored.
Example
use ;
foo
attrs
Specify the HTML attributes for the element. Useful for commonly used static attributes.
Attributes are specified as ident = "value"
pairs.
Example
use ;
foo
attr
Specify a single attribute for the element.
Useful either when the attribute must be dynamically created or when its key cannot be written as a valid rust ident.
Example
use ;
foo
format
Format the content of the element using the provided format string.
Valid only on fields.
Example
use ;
Hi, I'm foo
map
Map the value of this field using an expression before writing the HTML.
This attribute applies only to HTML generation. Any field annotated with this will still use its original value when used in format strings in other attributes.
Valid syntax is variable => { /* do stuff with variable */ }
Example
use ;
let el = El ;
let html = r#"<div><p id="foo">Empty: false</p></div>"#;
assert_eq!;
Empty: false
default
Valid only on Option
s and fields that are not annotated with map
.
Normally, Option
s annotated with element
which are None
during HTML generation will be completely ignored and no DOM object will get created.
This attribute ensures the element gets created with the specified content even when the field is None
.
Example
use ;
let el = El ;
let html = r#"<div><p>foo</p></div>"#;
assert_eq!
foo
before/after
Insert/append strings before/after the content of an element.
Useful for inserting elements inside the parent element, especially when dealing with lists.
When used with list
, it inserts the given string before/after the first/last element.
Example
use ;
let el = El ;
let html = r#"<div><section><h1>Win a million dollars</h1>You are the chosen one<button>Do it</button></section></div>"#;
assert_eq!
Win a million dollars
You are the chosen one
Do it
nest
Use on any field that's a struct implementing HtmxElement
. Calls the underlying implementation in the context of the current struct.
Example
use ;
let el = El ;
let html = r#"<div><section><p>qux</p></section></div>"#;
assert_eq!
qux
list [(nest)]
Use on list collections. Valid with any iterable whose item implements Display
.
Create the specified element for each item in the list, using the item's value for its content.
When used as list(nest)
, calls to_htmx()
for each item in the list and writes it to the final HTML.
Example
use ;
let el = El ;
let html = r#"<ul><li>foo1</li><li>foo2</li><li><p>qux</p></li></ul>"#;
assert_eq!
foo1
foo2
qux
hx, hx_method
hx_*
attributes correspond to the available AJAX methods in htmx. They also support format strings, i.e. can be dynamically generated using the fields of the struct in question.
hx
is pretty much the same as attrs, except it prepends hx
to every key.
If you need dynamic values, use attr.
Example
use ;
let el = El ;
let html = r##"<div hx-get="/foo/bar" hx-target="#420" hx-swap="innerHtml"><p id="420">Meaning of life: 420</p></div>"##;
assert_eq!
Meaning of life: 420
urlencode
Use when you need to encode url parameters.
Example
use ;
let el = El ;
let html = r##"<div hx-get="/foo/my%20bar"></div>"##;
assert_eq!
More examples
I am a p! Hello World!
Always keep it 69
Todo List:
- Basic HTML
- Ajax attributes
- Attributes for collections for ez lists
- [] Response trait
- [] hx headers for response trait