Require braces around the body of an element #137
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Maud now requires braces around the body of an HTML element. For example, this code
must now be written as
This change was made to address usability issues, as well as improve visual harmony with surrounding Rust code.
Rationale
Omitting the braces around an element body can reduce clutter. The feature is also easy to implement, and does not introduce ambiguity in the formal grammar.
However, over the last few months I have found many arguments for removing the feature:
In html attribute
for
(on label tag) doesn't parse #117, a user questioned whylabel "Email" for="email"
didn't work. If we had required braces around the body, then it would be clear whylabel { "Email" } for="email"
wouldn't parse.A void element (e.g.
input type="text";
) is always terminated by a semicolon. But an element with a single node in its body (e.g.p "hello"
) does not require a semicolon. This is inconsistent with Rust syntax in general, which requires semicolons on statements unless they have return values or end with a braced block.The shorthand syntax also hides errors. If the user omits the trailing semicolon on a void element, then Maud will emit an extra end tag. For example, this code:
results in the following HTML:
It's possible for the library to detect and warn about these cases, but we should aim to avoid this situation in the first place.
FAQ
Do we need a deprecation period for this change?
I don't think so. Template engines tend to be direct dependencies, not transitive ones (like Serde), so it should be easy to pin an older version if you're not ready to deal with the breakage yet.
I have another question. Should I ask it here?
Go for it! 😄