-
-
Notifications
You must be signed in to change notification settings - Fork 85
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 inline tags #755
Support inline tags #755
Conversation
Adding support of inline tag declaration Examples: ``` h1 'Hello' ``` will render: ``` <h1>Hello</h1> ``` ``` div do h1 'Hello', class: 'title' end ``` will render: ``` <div><h1 class="title">Hello</h1></div> ``` Some specific cases where block passed as inline element should be properly wrapped because of ruby blocks calls sequence rules ie: ``` def div_block proc do div attr: 'fourth' do 'Hello' end end end def view_template div attr: 'first' do div attr: 'second' do div(div_block, attr: 'third') end end end ``` In this particular case `div_block` content should be wrapped to a proc to keep execution sequence
7077228
to
e8e5a15
Compare
lib/phlex/elements.rb
Outdated
inline_block = content.is_a?(Proc) ? content : proc { content } | ||
yield_content(&inline_block) |
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.
I think we should probably do something more like this:
case content
when Proc
yield_content(&content)
when String
plain(content)
else
raise ArgumentError
end
In the case that we're given a string, we can completely avoid the block which should mean it’s faster.
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.
Good catch.
I moved processing method to SGML
class. I hope it's ok.
Tests also improved
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.
Pls let me know if I need to update documentation somewhere. Thanks
db28a5c
to
5682854
Compare
5682854
to
471adcc
Compare
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.
I like this change and it’s non-breaking. We’ve never come up with anything else to use the positional argument for so I think this makes sense. I’ve pinged @willcosgrove for his thoughts though as this is a huge change and one that would be difficult to reverse.
@joeldrapper convinced me that this is a good change 😄 I think we should not allow nesting using this though. h1(em("Hello")) # 💥 And I think wrapping it in a proc (while it does fix the issue of the elements not getting added to the buffer at the right time) is not something we should support. |
It's actually pretty specific cases what I faced when tests inline tags in my application. I doubt what it frequently used( table do
tr do
th 'Name'
th 'Age'
end
@users.each do |user|
tr do
td user.name
td user.age
end
end
end |
Okay final feedback on this: Let's make sure we raise if you use a positional argument and a block, e.g. h1("Hello") { "World" } Let’s make sure we raise if you pass h1(h2("Hi"))
We won’t be able to use module Phlex
Null = Object.new.freeze
end Then we can check against that
Let’s also remove the Finally, while extracting methods is generally good practice, I expect we will get better performance if we just inline the stuff in |
b6fc4ff
to
09cb7ef
Compare
@joeldrapper Pls take a look ;) |
Based on issue
Adding support of inline tag declaration
Examples:
will render:
will render: