Branching with if, elseif, and else
#[derive(Oxiplate)]
#[oxiplate = "template.html.oxip"]
struct YourStruct {
count: i64,
}
print!("{}", YourStruct {
count: 19,
});
<p>
{%- if count < 0 -%}
{{ count }} is negative
{%- elseif count > 0 -%}
{{ count }} is positive
{%- else -%}
{{ count }} is zero
{%- endif -%}
</p>
<p>19 is positive</p>
if let and elseif let
Similarly to Rust,
let can be used in if and elseif statements.
#[derive(Oxiplate)]
#[oxiplate_inline(html: r#"
<p>
{%- if let Some(count) = count -%}
The count is {{ count }}.
{%- else -%}
No count provided.
{%- endif -%}
</p>
"#)]
struct YourStruct {
count: Option<i64>,
}
assert_eq!("<p>The count is 19.</p>", format!("{}", YourStruct {
count: Some(19),
}));
Additionally,
for single-variable situations where borrowing is acceptable,
the = variable part can be skipped
(= &variable will be inserted automatically):
#[derive(Oxiplate)]
#[oxiplate_inline(html: r#"
<p>
{%- if let Some(count) -%}
The count is {{ count }}.
{%- else -%}
No count provided.
{%- endif -%}
</p>
"#)]
struct YourStruct {
count: Option<i64>,
}
assert_eq!("<p>The count is 19.</p>", format!("{}", YourStruct {
count: Some(19),
}));