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

Support pipe in Serbea 2.0, add new pure Ruby template syntax (WIP) #817

Merged
merged 19 commits into from
Apr 12, 2024

Conversation

jaredcwhite
Copy link
Member

@jaredcwhite jaredcwhite commented Oct 10, 2023

Serbea 2.0 brings with it a new pipe helper that you can use in any Ruby context (including ERB). Example in a PORO:

class PipelineExample
  include Serbea::Pipeline::Helper

  def output
    pipe("Hello world") { upcase | split(" ") | test_join(", ") }
  end

  def test_join(input, delimeter)
    input.join(delimeter)
  end
end

This PR brings that into Bridgetown templates and components, as well as adds a brand-new enhanced "HTML-in-Ruby" syntax based on heredocs. (Update: I've extracted this out to a standalone gem, Streamlined, that Bridgetown will then import.)

Syntax example:

class RubyHtmlText < Bridgetown::Component
  def template
    html -> { <<~HTML
      <p>This is #{text -> { "<b>escaped!</b> "}}</p>
      #{html "_yipee", -> { markdownify | sub("yipee", "yay") }}
    HTML
    }
  end
end

You can think of the new text, html, render, and html_map helpers for HTML-in-Ruby as somewhat analogous to "tagged template literals" in JavaScript. HTML-in-Ruby is an alternative template format to ERB and Serbea, and can be used in any .rb resource template as well as PORO components as in the example above.

A RuboCop linter will be provided so that any HTML or MARKDOWN heredoc with interpolated expressions not starting with an approved helper can be flagged. This is to ensure security against XSS…because Ruby doesn't provide any way to process interpolated expressions before they're inserted into the string, we have no way of HTML escaping content by default. By intentionally choosing the text helper, you get an escaped string. Or by using html/render/etc., it will not escape.

The file-based routes syntax is also improved, with a new Ruby front matter style available so you can write route handlers like:

render_with do
  layout :page
  title "Access Your Account"
end
  • Tests
  • Extract linter to rubocop-bridgetown gem
  • Extract Ruby HTML template syntax to standalone gem, make dependency of Bridgetown
  • Documentation
  • Breaking change re: Roda cookies! (see comment)

@render
Copy link

render bot commented Oct 10, 2023

@render
Copy link

render bot commented Oct 10, 2023

@jaredcwhite jaredcwhite added this to the 1.4 milestone Oct 10, 2023
@jaredcwhite
Copy link
Member Author

Note: this PR includes one breaking change to the default cookies setup for Roda (to set the cookie path to /…otherwise cookies sort of seem broken because they don't work across the whole site).

@jaredcwhite
Copy link
Member Author

I'm going to go ahead and merge this in, and create some follow-up issues for the documentation, etc.

@jaredcwhite jaredcwhite merged commit 54e56ef into main Apr 12, 2024
0 of 3 checks passed
@jaredcwhite jaredcwhite deleted the support-serbea-2.0 branch April 12, 2024 04:06
@jaredcwhite jaredcwhite mentioned this pull request Apr 12, 2024
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.

1 participant