utkarshkukreti / markup.rs

A blazing fast, type-safe template engine for Rust.
Apache License 2.0
350 stars 14 forks source link

optional attributes #14

Closed miguelski closed 3 years ago

miguelski commented 3 years ago

problem: i am migrating a project from horrorshow currently there are some Option variables for example let css: Option<String> = None; but i don't find a way to specify that if css is None don't create the html attribute div[class=css] creates <div class=""></div> but i want create when css is None <div></div> i know i can do an if conditional like this

@if let Option(css) = css{
div[class=css]
}else
{
div{}
}

but this is not practical when i have a lot of optional attributes

horrorshow have this sintaxis div(class?=css) and i get what i want <div></div> when css is None

is there something similar that i missing? or it would be a new feature?

ZaneHannanAU commented 3 years ago

May be possible with bailout conditions div[class=css?] and have the codegen put it in its own

let _: Option<()> = {
  Some(writer.write("<div class=\"{}\">", css?))
}.transpose()?;

but seems like a fair bit more work and codegen

ZaneHannanAU commented 3 years ago

@ZaneHannanAU misread the original...isn't quite so simple. The class="" shouldn't be too bad outside of looking like php.

miguelski commented 3 years ago

reading the code it should work, i make it work but if the variable is Option we need to use as_ref() and if the variable is Option<& str> use otherwise it doesn't work, same for false boolean if it is not used with don't work.

image

miguelski commented 3 years ago

https://github.com/utkarshkukreti/markup.rs/blob/4dfc1275ffae79714539004c1762f3b3f9430989/markup-proc-macro/src/generate.rs#L137

i think the issue is in the above methods. is_none when used &Option is always false and is_false is always false if a &boolean is passed instead a boolean

utkarshkukreti commented 3 years ago

Thanks for reporting the issue and figuring out the solution @miguelski! Can you please try out the latest version from git to confirm this fixes your code before I make a release (I have added tests, but just to be sure)?

[dependencies]
markup = { git = "https://github.com/utkarshkukreti/markup.rs" }
miguelski commented 3 years ago

working for me, thank you image

utkarshkukreti commented 3 years ago

Awesome, published 0.12.1.