forge-rsx
forge-rsx is a Rust macro library for declarative, JSX-like HTML generation. It allows you to write HTML structures with embedded logic, attributes, nested tags, loops, and more, directly in Rust code with a concise syntax.
Features
- Declarative HTML macro:
rsx!macro - Supports nested tags, attributes, loops, and embedded expressions
- Indentation-aware formatting
- String literal and identifier attributes
- Flexible syntax for defining complex HTML structures
Usage
Run the following Cargo command in your project directory:
cargo add forge-rsx
Or add forge-rsx as a dependency in your Cargo.toml:
[]
= "MAJOR.MINOR.PATCH" # Replace with the latest version
In your Rust code, import the macro:
use rsx;
Macro Variants
lined: produces HTML without indentation or line breaks (single-line output)btfy0: uses 0 spaces (no indentation, minified output)btfy2: Indentation with 2 spaces per level.btfy4: Indentation with 4 spaces per level.tabed: Indentation with 2 spaces per level. (tabed = btfy2)
Examples:
use rsx;
rsx!;
rsx!;
rsx!;
rsx!;
Examples
Basic Tag with Content
use rsx;
// 1. First style: using parentheses => ()
let greeting1 = rsx!;
println!; // output: <span>Hello, World!</span>
// 2. Second style: using braces => {}
let greeting2 = rsx! ;
println!; // output: <span>Hello, World!</span>
Nested Tags
use rsx;
let nested_html = rsx!;
println!;
Attributes
// This example demonstrates:
// 1. Attribute quoting: Forge-RSX uses double quotes. Use
// single quotes inside JS/Alpine strings to maintain HTML validity.
// 2. Boolean attributes: Attributes assigned `true` render as keys only.
// Attributes assigned `false` are omitted.
// 3. Formatting: The `lined` mode creates compact HTML without extra whitespace.
use rsx;
Loop Example
use rsx;
let users = vec!;
let list_html = rsx!;
println!;
Code Tag
use rsx;
Full Complex Example
use ;
License
MIT License
Notes
- The macro supports attributes with string literals and identifiers.
- Nested tags are handled with recursive macro calls.
- Looping constructs generate repeated content.
- Content inside braces
{}can contain any Rust expression that implementsDisplay.