Closed rben01 closed 7 months ago
Not sure if this would apply to your real use case, but for this exact example, you can use a dynamic element name ${expr}
:
markup::define! {
Body(use_div: bool, n: usize) {
@let name = if *use_div { "div" } else { "span" };
@let x = "x";
${name} {
@for _ in 0..*n { @x }
}
"it's a " @name
}
}
Does this help?
Or you can use markup::new!
to define an anonymous template:
markup::define! {
Body(use_div: bool, n: usize) {
@let x = "x";
@let for_loop = markup::new! {
@for _ in 0..*n { @x }
};
@if *use_div {
div { @for_loop }
"it's a div"
} else {
span { @for_loop }
"it's a span"
}
}
}
Thanks! I hadn't considered the inline use of markup::new!
but that's exactly what I'm looking for.
I have code structured as follows:
Naturally I want to deduplicate the two identical loops, but I can't figure out what the most natural way to do this is.
Attempts that don't compile:
I suppose I could define a new struct in the
define!
and then render it as follows, but I'm hoping for something a bit simpler if possible (I don't want to have to do this every time I have a bit of repeated work).