Polymorphic Phlex HTML tags #575
Replies: 2 comments 1 reply
-
|
Another thought: instead of a def html_attributes(tag)
case tag
when :input, :button
{ name: field_name(@name), id: field_id(@name), value: @value }
when :textarea
{ name: field_name(@name), id: field_id(@name) }
end
endThe advantage this would have is keeping all html attribute generation into one method, making it easier to find and more likely to stay together in a codebase. This approach would also be advantageous for broader HTML attributes. For example, all Ruby ActiveRecord objects might implement something like this on the ApplicationRecord model: def html_attributes(tag)
case :a
{ title: title, href: url_for(self) }
else
{ title: title }
end
endWhen this model is passed into a tag to render, it would always inject a It’s also possible to include the attributes provided to the Phlex HTML call so the input field(:name), validation: falsePhlex would then call One thing I don’t like about this is the same approach would be achieved by the following: input field(:name, validation: false)Which would set the state on the object that fields returns that generates the desired HTML attributes. The same effect could be achieved with |
Beta Was this translation helpful? Give feedback.
-
|
I opened a Draft PR at #576 to continue the discussion. The demo app has been updated to work against this proposed API. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've been building a demo application for Phlex at https://github.com/rocketshipio/oxidizer-demo that's "Phlex to the Max"
One idea I ran into is one for what I call polymorphic tags. Here's the idea: let's say you're building a form helper like this:
The implementation is at https://github.com//rocketshipio/oxidizer-demo/blob/6e30bd2197a2064714e8ad5edd88ec018f5c7dcd/app/views/phlex_form.rb#L62-L79
In this case, the
fieldmethod returns aFieldclass that responds tolabel_attributesandinput_attributes. The Phlexinputandlabelmethod checks to see if their respective attributes method exists, and if they do, it calls them and uses those attributes.This could work well for hyperlinks and images too. For example, let's say a user model has a
profile_image_urlthat represents the user. On the user class, the following could be implemented:From our Phlex code we could then pass an instance of a user straight into an
imgtag and it will know what to do:Another proposal I'd add for polymorphic tags is accepting a component as the first argument and rendering it as an immediate child. For example, a list could be rendered like this:
This could be similarly useful for Ruby classes, like User, that want to have a component associated with them:
I suppose the same method checks could happen as above: if an
li_componentmethod is present on the User object, it would render that component. I think this might be a little "too polymorphic" since you'd have to wonder from looking at an object if it's returning attributes or a component. To keep things sane, I'd keep the*_componentmethod check out and just make it possible to pass a component into the first argument and it render as a child.Beta Was this translation helpful? Give feedback.
All reactions