bridgetownrb / serbea

The Ruby template engine you didn't realize you needed. Until now.
https://www.serbea.dev
MIT License
42 stars 1 forks source link

Escape the escape: Make ruby context default? #1

Closed redbar0n closed 3 years ago

redbar0n commented 3 years ago

I was reading the examples on the website, e.g.:

(PS: don’t know how to syntax highlight .serbea in github properly, so its interpreted as a .rb file here..)

{%= form classname: "checkout" do |f| %}
  {{ f.input :first_name, required: true | errors: error_messages }}
{% end %}

{%= render "box" do %}
  This is **dope!**
  {%= render "card", title: "Nifty!" do %}
    So great.
  {% end %}
{% end %}

Would it be possible to invert the escaping, so one would have to escape less?

So it would be like this:

form classname: "checkout" do |f|
  f.input :first_name, required: true | errors: error_messages
end

render "box" do
  {% This is **dope!** }
  render "card", title: "Nifty!" do
    {% So great. }
  end
end
jaredcwhite commented 3 years ago

That's sort of what Arbre does: https://activeadmin.github.io/arbre/

This is an interesting add-on as well: https://github.com/joshleblanc/cedar

My personal preference is to start with markup and work template code in from there…Serbea also works great with Markdown or any other text format (just like ERB, Liquid, etc.). But there are certainly cases where a Ruby DSL would be an ideal alternative.

Thinking out loud…there could be a solution where a special directive would drop into a Ruby DSL automatically. Like:

<p>Hi, I'm HTML</p>

{%@arbre
  panel "Hello World", id: "my-panel" do
      span "Inside the panel"
      text_node "Plain text"
  end
%}

<p>Done!</p>

That'd be equivalent to:

<p>Hi, I'm HTML</p>

{%= Arbre::Context.new do
  panel "Hello World", id: "my-panel" do
    span "Inside the panel"
    text_node "Plain text"
  end
end %}

<p>Done!</p>

Could probably get even more advanced with capturing Serbea sub-templates via a capture inside the DSL block, but I'd have to test that out…anyway, lots of possibilities. =)

Thanks for the feedback!

redbar0n commented 3 years ago

Thanks for a great and quick reply!

I didn't think about Arbre.

I think there is some good reasons (3-4 min from that point) to have default context in Ruby, since markup is underpowered (although by design).

I think for familiarity/intuitiveness it's important to have the markup syntax not too far removed from HTML. Imo, JSX hit a sweet spot there.

Dropping into a Ruby context intermittently would feel a bit like a mode ("no modes").

Thinking out loud..

One alternative would be the JSX-like:

render "box" do
  <h1>Header text</h1>
  <span>This is <b>dope!</b></span>
  render "card", title: "Nifty!" do
    <p>So great.</p>
  end
end

Or with Arbre maybe it would be like:

render "box" do
  h1 { "Header text" }
  span { text { "This is" }, b { "dope!" } }
  render "card", title: "Nifty!" do
    para { "So great." }
  end
end

But since having to stringify all text output is annoying, maybe instead:

render "box" do
  h1 { Header text }
  span { text { This is }, b { dope! } }
  render "card", title: "Nifty!" do
    para { So great. }
  end
end

With Cedar it might be:

render "box" do
  h1 "Header text"
  span do
    text "This is"
    b "dope!"
  end
  render "card", title: "Nifty!" do
    para "So great."
  end
end

But really, I think I prefer the first, JSX-like option. It feels like just Ruby+HTML, without any indirection (through a DSL). FWIW.

jaredcwhite commented 3 years ago

I know somebody's who's working on a JSX-style template language that commingles markup with Ruby…as much as I like the "cool" factor of it, I just fundamentally believe in a separation of concerns. I want my HTML to be HTML. I want my Ruby to be Ruby. But I definitely understand the appeal you present there. The great thing is, whether we're talking about Rails or Bridgetown, we can pick and choose which template languages we prefer. Yay!

redbar0n commented 3 years ago

That's ok. I myself got convinced by that Pete Hunt presentation that concerns should not be about technology but about purpose (aka. the feature, role or domain served). But I respect that some may disagree.

I just happpened to come across this interesting tweet thread by Jordan Walke about how JSX came about.

Who is that somebody you mentioned you knew?

I'd like to follow them on twitter or github to keep my eyes on that project too.

You can close this issue if you don't see a future in this particular idea. I just wanted to throw it out there, in case it could help.

jaredcwhite commented 3 years ago

Yeah, I don't know if it's officially "out in the wild" yet but here's the GH project: https://github.com/camertron/rux

For now I'll pursue the idea of optionally dropping into a DSL for cases where that's most useful, seems like a nice halfway point between the two worlds.