-
Notifications
You must be signed in to change notification settings - Fork 136
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
withClass should append class, not replace #73
Comments
Can you show me your code? The more idiomatic way to write it would probably be |
it's just that one sometimes has to add classes to the same element at different positions in the code, and replacing existing class names is practically never needed. a separate "addClass" method would be okay too. |
+1 |
@h2software can you show me a bigger code example where you want to use this? I understand the simple cases, but I really think you should use |
Sure... ContainerTag feedbackDiv = div();
boolean hasFeedback = false;
if (!feedback.isAnswerBlank(exerciseInput)) {
FeedbackStatus feedbackStatus = feedback.getFeedbackStatus(exerciseInput, key);
if (feedbackStatus != null) {
FeedbackType feedbackType = feedbackStatus.getFeedbackType();
// There is no appendClass, so we're adding form-control AGAIN
tag = tag.withClass("form-control " + feedbackType.getClassForFormControl());
hasFeedback = feedbackStatus.getFeedbackText() != null;
feedbackDiv = feedbackDiv.with(text(feedbackStatus.getFeedbackText())).withClass(feedbackType.getClassForFeedbackDiv());
}
} |
@h2software there isn't a lot of j2html in your example, but I understand why you want the method. I really want to nudge people towards a more declarative programming style when using j2html though (similar to how you would write actual HTML). There are currently no methods in j2html that append anything, so I'm reluctant to introduce this. It would be inconsistent, and it would encourage people to write very non-idiomatic j2html-code. I'd be happy to assist you in converting your code to idiomatic j2html if you're interested. |
No problem. Thank you for giving it some thought. |
Well, damn. That's a valid point, and a major oversight on my part. Unfortunately, this makes me want to change the behavior of |
Yes, loads of times. Here's an example (I've cut down complexity just to show
Please don't change the behaviour of the |
I won't change it anytime soon. I would need to run a survey first, and depending on the results, make the change for version 2 of the library. Your code would be a lot easier to read without using
I'll stop trying to convert you though. I've made a note of the inconsistency, and I'll think about what to do. |
No problem. Thank you. |
I think we should keep the behavior and even extend |
Just to note
|
I also hit this. For what it's worth, my assumption was definitely that |
@QuintinWillison do you think |
@tipsy yes, indeed. Though my analysis is likely flippant - you clearly have a much deeper understanding of the wider design picture. I was just commenting based on my context and setting - which, as we all do, carries lots of assumptions with it. Thanks. |
Forgive my naivety but I dont understand when you would ever want with() to replace rather than append. Since j2html is always constructing html to eventually be rendered in a html page wouldnt multiple calls to with() on an element that just mean you were overwriting an earlier call you made and therefore why make that first call in the first place. |
The idea behind j2html was to allow people to write declarative Java code with a 1-1 HTML mapping. I didn't anticipate people using it with mutable/temporary objects. As I said in a comment a little up, intended use is: tbody(
each(lines, line -> tr(
td((lines.indexOf(line) + 1) + ". " + line)
))
) Not intended use: ContainerTag tbody = tbody();
int lineIndex = 0;
for (String line : lines) {
ContainerTag tr = tr();
tr = tr.with(td((lineIndex + 1) + ". " + line));
tbody = tbody.with(tr);
lineIndex = lineIndex + 1;
} If you look at the examples on the website, all the code-snippets follows this declarative style where you never save a reference, and you never alter an object after its creation. I'm not saying people are wrong for not following this style, but that's why methods don't append ( |
You can use |
Until 1.3 I couldn't because of #97 but yes hopefully should now work |
Also hit this. |
We also hit this. |
Hit this as well, this is very surprising behaviour. Especially since A lot of times elements have a base set of classes and need some conditional classes added based on validation etc. like input(...)
.withClasses("form-control", "light")
.withCondClass(!validator.validate(field), "input-invalid") Sure its possible to stuff tenary expressions into the While changing the behaviour of |
@lambdaupb your example is one that convinces me that appending behavior is needed. I've favored the |
That would simplify code in quite a few cases.
The text was updated successfully, but these errors were encountered: