Skip to content
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

Add fallback to ease showing default content #78

Closed
wants to merge 2 commits into from

Conversation

kaspth
Copy link
Contributor

@kaspth kaspth commented Feb 1, 2023

Just a quick idea to provide ease providing fallback content, in case a section is optional.

Before:

<% if partial.header? %>
  <%= partial.header %>
<% else %>
  <h1>Default Header</h1>
<% end %>

After:

<%= partial.header.fallback do %>
  <h1>Default Header</h1>
<% end %>

Or the one liner in this simple case <%= partial.header.fallback tag.h1("Default Header") %>.

I haven't wrapped this up and I'm gonna sleep on this if any knits or issues pop up.

@kaspth
Copy link
Contributor Author

kaspth commented Feb 1, 2023

Technically, we already support this. Just for reference:

<% if header = partial.header.presence %>
  <%= header %>
<% else %>
  <h1>Default Header</h1>
<% end %>

@kaspth
Copy link
Contributor Author

kaspth commented Feb 1, 2023

Suppose we could also support <%= partial.header.fallback.h1 "Default Header" %> by doing something like this:

def fallback(content = nil, &block)
  presence or begin
    case
    when content.present? then content
    when block_given?     then @view_context.capture(&block)
    else
      @view_context.tag # Return the plain tag context so people can call `h1` etc on it.
    end
  end
end

Though I don't know if that's worth it really.

@domchristie
Copy link
Contributor

I quite like the Rails render collection fallback API, so I wonder if there's some inspiration to be taken from this?

<%= render(partial: "ad", collection: @advertisements) || "There's no ad to be displayed" %>

@kaspth
Copy link
Contributor Author

kaspth commented Feb 2, 2023

@domchristie yeah, that works because render executes immediately whereas we defer until ERB calls to_s, so we could support the same with:

<%= partial.header.presence || "Default Header" %> <%# This already works today %>
<%= partial.header.presence or capture do %> <%# Here's the issue, that users need to call `capture` when they use a block fallback. %>

Reading this, I think I'm going to change it so that we alias presence to call to trim this a bit.

@kaspth kaspth mentioned this pull request Feb 2, 2023
@kaspth
Copy link
Contributor Author

kaspth commented Mar 13, 2023

@domchristie I ended up fiddling with something in the same ballpark in #84. And looking at this again the fallback does seem a bit fiddly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants