-
Notifications
You must be signed in to change notification settings - Fork 409
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
Strip trailing whitespace in templates #1379
Conversation
Wasn't this solved by #913 and rails/rails#42279? 🤔 |
@Spone If I'm understanding the VC and Rails PRs, then I think this is a separate problem. In this case, stripping newlines from the compiled ERB won't help because the ERB contains |
@camertron, makes sense, thanks for clarifying! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I lack context, but is there any risk of this breaking existing templates/behavior that would warrant a config option + deprecation warning?
@BlakeWilliams hmm that's a really good call re: email templates. Maybe it makes sense to put the config option on |
Hmm. I'd love to avoid a config option long-term. Is there any way we can test this hypothesis? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs docs beyond API
Use a default value for the boolean flag so you can simply call `strip_trailing_newlines` in your component. Co-authored-by: Joel Hawksley <joel@hawksley.org>
We discussed the config option internally and came to consensus that a per-component class method is ok 👍 eg. class MyComponent < ViewComponent::Base
strip_trailing_whitespace
end |
* Strip trailing newlines in templates * Use rstrip so only trailing whitespace is trimmed * Add changelog entry * Add test demonstrating trailing newline behavior * Use config option * Fix linting issues * Update lib/view_component/base.rb Use a default value for the boolean flag so you can simply call `strip_trailing_newlines` in your component. Co-authored-by: Joel Hawksley <joel@hawksley.org> * Add additional docs; rename to strip_trailing_whitespace * Fence code blocks in base.rb * Move trailing whitespace docs to templates docs Co-authored-by: Joel Hawksley <joel@hawksley.org>
* Strip trailing newlines in templates * Use rstrip so only trailing whitespace is trimmed * Add changelog entry * Add test demonstrating trailing newline behavior * Use config option * Fix linting issues * Update lib/view_component/base.rb Use a default value for the boolean flag so you can simply call `strip_trailing_newlines` in your component. Co-authored-by: Joel Hawksley <joel@hawksley.org> * Add additional docs; rename to strip_trailing_whitespace * Fence code blocks in base.rb * Move trailing whitespace docs to templates docs Co-authored-by: Joel Hawksley <joel@hawksley.org>
What are you trying to accomplish?
Today a co-worker raised an issue with the
LinkComponent
in primer/view_components where it appeared the component was adding a newline to the end of the rendered output. It turned out there was a newline at the end of the corresponding template, which the ERB compiler faithfully turned into@output_buffer.append("\n")
. Most of the time this isn't a problem, but it's common to includeLinkComponent
in a sentence. If it comes at the end of the sentence, there will be a space between the link text and the period.IMHO this is pretty surprising behavior considering that the *nix standard is to leave a single trailing newline in all files. In almost all cases I don't think anyone will notice if view_component were to remove trailing whitespace, but I'm curious to hear from others.
What approach did you choose and why?
I added an
.rstrip
call in the compiler that trims trailing whitespace from the template before compiling it.