-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support toggling attributes that have a value #283
Comments
Thanks for mapping this out! I've had this requested elsewhere (#240 (comment)), and Before going into syntax – I wonder if we should only support My reasons are:
@DataTriny @zopieux what do you think? |
Hi @lambda-fairy , Looking back at my first comment, the second alternative would conflict with |
I like the suggestion of only accepting To sum up, do I understand correctly that it would be: let toggle = true;
html! { checkbox checked[toggle] {} } // current syntax for a valueless attr
// ↑ Would this syntax be deprecated?
// Non-Option
let id = 42;
html! { div data-id=(id) {} } // <div data-id="42">
let id = "42";
html! { div data-id=(id) {} } // <div data-id="42">
// Option<not bool>
let id = Some(42usize);
html! { div data-id=(id) {} } // <div data-id="42">
let id: Option<usize> = None;
html! { div data-id=(id) {} } // <div>
let flag = Some("false");
html! { div data-flag=(flag) {} } // <div data-flag="false">
let flag = Some("");
html! { div data-flag=(flag) {} } // <div data-flag="">
// Option<bool>.
let toggle = Some(true);
html! { checkbox checked=(toggle) {} } // <checkbox checked>
let toggle = Some(false);
html! { checkbox checked=(toggle) {} } // <checkbox>
let toggle: Option<bool /* not that it matters though */> = None;
html! { checkbox checked=(toggle) {} } // <checkbox> |
To be more precise, my proposal only involved passing |
Ok @DataTriny, so if my understanding is correct:
While that makes sense, don't you think point 2. would make |
Hi @zopieux , Since my original proposal didn't target valueless attributes, I wasn't suggesting to get rid of the So to go through all your points:
|
That makes sense. Backward compatibility notwithstanding, I must admit keeping the toggle syntax sounds important so that both I'm not sure we need 4. in that situation. So yeah, big +1 on that proposal! |
Thanks for the proposal! I would make two minor changes:
So here's what I'm thinking:
If that sounds good to you @DataTriny @zopieux, would one of you like to try implement it? 🙂 |
I can try, but I'll be away from my development workstation for a few days :-) Thanks for weighting in, I'm fine with your latest suggestions. |
Hello, and thank you for taking the time to investigate this proposal. Not being familiar at all with Rust's procedural macros, I started doing some research which led me to think that overloading the existing syntax would probably be hard. Introducing the Do you have more knowledge on this @lambda-fairy ? I can also try to implement it, once this point is resolved. |
@DataTriny yep that's right. In Rust, macro expansion happens before name resolution and type checking. So information from the latter phases won't be available. I don't think that will be a problem in our case, though. Because if we see If you're curious about how an overloading solution works, check out the markup.rs code (macro, runtime). It works around the lack of type information by putting the decision in the |
Introduces the `attr=[value]` syntax that assumes `value` is an Option<T>. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an Option<T>. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an Option<T>. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an Option<T>. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes lambda-fairy#283.
* Add support for Option<T> attributes Introduces the `attr=[value]` syntax that assumes `value` is an `Option<T>`. Renders `attr="value"` for `Some(value)` and entirely omits the attribute for `None`. Implements and therefore closes #283. * Call `Generator::splice` directly * Handle struct literals (edge case) Co-authored-by: Chris Wong <[email protected]>
Background
It is already possible to toggle empty attributes like so:
On most cases (if not all), adding an empty value to an attribute will do the same as not adding anything. But if you are dynamically building an element with multiple optional attributes, the generated HTML will be unnecessary longer.
Proposition
I would suggest combining the syntax for toggling empty attributes and the syntax for defining attributes with a value.
Therefore, it would be possible to write something like this:
Alternatives
I don't have a preference for one or the other, they both make sense and should not introduce a breaking change in the syntax parsing logic.
Option<T>
:This makes for a shorter syntax, but might be harder to read.
What do you think? If you are interested, I already have a PR implementing the first solution.
The text was updated successfully, but these errors were encountered: