use rstml_component::{write_html, HtmlAttributeValue, HtmlComponent, HtmlContent, HtmlFormatter};
#[derive(HtmlComponent)]
struct NavBar;
impl HtmlContent for NavBar {
fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result {
write_html!(formatter,
)
}
}
#[derive(HtmlComponent)]
pub(crate) struct Template
where
T: Into,
A: HtmlAttributeValue + HtmlContent + Clone,
C: HtmlContent,
{
title: T,
attribute: A,
children: C,
}
impl HtmlContent for Template
where
T: Into,
A: HtmlAttributeValue + HtmlContent + Clone,
C: HtmlContent,
{
fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result {
write_html!(formatter,
{self.title.into()}
<>
"1"
Hello world with spaces
"3"
{self.attribute}
>
{self.children}
)
}
}
#[derive(HtmlComponent)]
struct Page
where
T: Into,
{
title: T,
heading: String,
}
impl HtmlContent for Page
where
T: Into,
{
fn fmt(self, formatter: &mut HtmlFormatter) -> std::fmt::Result {
write_html!(formatter,
{self.heading}
"This is a test"
)
}
}
#[test]
fn test_output() {
let page = Page {
title: "Hello",
heading: "Hello world".into(),
};
let output = page
.into_string()
.expect("formatting works and produces valid utf-8");
let expected = r#"
Hello
1
Hello world with spaces
3
world
Hello world
This is a test
"#
.trim()
.lines()
.map(|l| l.trim())
.collect::();
assert_eq!(output, expected);
}