
Template engine for HTML in Rust
cargo add cercis
Using examples
Formatting
Format the data into a string as in format!() macro
all data is transferred to the template by reference
use cercis::prelude::*;
fn main() {
let name = "Boris";
let page = rsx!(h1 { "Hello {name}!" });
println!("{}", page.render())
}
Attributes
Attributes are written before the tag content as tag: value
use cercis::prelude::*;
fn main() {
let text_id = "paragraph";
let text = "Lorem ipsum";
let page = rsx!(div {
class: "container",
h1 { "Hello World!" }
p {
id: "{text_id}",
"{text}"
}
});
println!("{}", page.render())
}
If branching
Using the usual Rust if syntax, you can create branches
use cercis::prelude::*;
fn main() {
let num = 8;
let page = rsx!(div {
if num == 9 {
span { "Num is 9" }
}
if num == 8 {
span { "Num is 8" }
}
});
println!("{}", page.render())
}
Loops
Using the usual Rust for in you can create loops
use cercis::prelude::*;
fn main() {
let names = vec!["Boris", "Polina", "Igor"];
let page = rsx!(ol {
for name in names {
li { "{name}" }
}
});
println!("{}", page.render())
}
Component with params
Parameters are declared as normal function parameters
use cercis::prelude::*;
fn main() {
let text = "Lorem ipsum";
let page = rsx!(div {
MyComponent {
text: text
}
});
println!("{}", page.render())
}
#[component]
fn MyComponent<'a>(text: &'a str) -> Element {
rsx!(p { "{text}" })
}
Component with option params
If the parameter is an option, then you can omit it when calling the component
use cercis::prelude::*;
fn main() {
let text = "Lorem ipsum";
let page = rsx!(div {
MyComponent {}
MyComponent {
text: text
}
});
println!("{}", page.render())
}
#[component]
fn MyComponent<'a>(text: Option<&'a str>) -> Element {
let text = text.unwrap_or("empty");
rsx!(p { "{text}" })
}
Component with children
the component can accept elements example: Element<'a> and children if you name the variable children: Element<'a>
all Element types are optional
use cercis::prelude::*;
fn main() {
let text = "Lorem ipsum";
let page = rsx!(div {
MyComponent { span { "children" } }
MyComponent {
text: rsx!(p { "{text}" }),
span { "children" }
}
MyComponent { text: rsx!(p { "my text" }) }
});
println!("{}", page.render())
}
#[component]
fn MyComponent<'a>(text: Element<'a>, children: Element<'a>) -> Element {
rsx!(div {
class: "container",
div { text }
children
})
}
Full page
use cercis::prelude::*;
fn main() {
let names = vec!["Boris", "Polina", "Igor", "Nikita"];
let peoples = rsx!(ol {
for name in names {
li { "{name}" }
}
});
let page = rsx!(Page {
title: "Cercis",
h1 { "Peoples:" }
peoples
});
println!("{}", page.render())
}
#[component]
pub fn Page<'a>(title: Option<&'a str>, head: Element<'a>, children: Element<'a>) -> Element {
const META_CONTENT: &str = "witdh=device-width, initial-scale=1.0";
rsx!(
doctype {}
html {
head {
meta { charset: "UTF-8" }
meta {
name: "viewport",
content: "{META_CONTENT}",
}
head
if let Some(title) = title {
title { "{title}" }
}
}
body { children }
}
)
}
If you have any problems create issue