-
Hey everybody, I've been trying to work on a lighter ERB syntax that's auto-closing based on knowing we're working with HTML. I'm using backslashes, and since it's meant to be a superset on ERB for ease of adopting, I've called it BRB as in be-right-back to ERB. It's not as extreme as HAML or SLIM. Here's the before/after of what I'm thinking: <% posts.each do |post| %>
<h1><%= post.title %></h1>
<% end %>
\posts.each do |post|
<h1>\= post.title</h1>
\end As I've been playing with it and gotten used to it, it starts to feel really good to read and write, but I may be biased! 😄 Here's more of an explainer of the aim: # BRB aims to be a simpler syntax, but still a superset of ERB, that's aware of the context we're in: HTML.
#
# We're replacing <% %>, <%= %>, and <%# %> with \, \= and \# — these are self-terminating expressions.
#
# We recognize these contexts and convert them to their terminated ERB equivalent:
#
# 1. A plain Ruby line: \puts "yo" -> <%puts "yo" %>
# 2. Within a tag: <h1>\= post.title</h1> -> <h1><%= post.title %></h1>
# 3. Within attributes at the end: <h1 \= post.options></h1> -> <h1 aria-labelledby="title"></h1>
# 4. Within attributes:
# <h1 \= post.options \= class_names(active: post.active?) data-controller="title"></h1> ->
# <h1 aria-labelledby="title" class="active"data-controller="title"></h1> Additionally, I've been exploring the idea of having preprocessing sigils to simplify common actions we do in HTML templates. Here's what they are: # Here's BRB with preprocessing sigils. Sigils aim to make actions common in an HTML context easier to write.
# At template compile time the sigils are `gsub`'ed into their replacements.
#
# They follow this syntax, here `sigil_name` is the name of the Sigil:
#
# \sigil_name # A sigil with no arguments
# \sigil_name(< ruby expression >) # A sigil with arguments, must be called with ()
#
# Examples:
#
# \class(active: post.active?) -> class="<%= class_names(active: post.active?) %>"
# \attributes(post.options) -> <%= tag.attributes(post.options) %>
# \p(post.options) -> <%= tag.attributes(post.options) %>
# \data(post.data) -> <%= tag.attributes(data: post.data) %>
# \lorem -> Lorem ipsum dolor sit amet…
#
# There's also a `t` sigil, but that's a little more involved since there's some extra things to condense:
#
# \t.message -> <%= t ".message" %>
# \t Some bare words -> <%= t "Some bare words" %> # Assumes we're using a gettext I18n backend, coming later! I've been working on a proof-of-concept implementation over here: bullet-train-co/nice_partials#91 Currently, I'm using regexes internally, but that'd probably need to be replaced with a There'd also be a fork of https://github.com/tree-sitter/tree-sitter-embedded-template for syntax highlighting in editors. I'm really curious to hear what you think and answer questions! Of course feel free to close this if it's not a fit at all. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hey @kaspth, thanks for bringing BRB to our attention, and apologies it's taken a long time for a maintainer to respond! I assume you created this discussion with the goal of hearing from the ViewComponent community, and not to ask if BRB support could be added to the framework. For those unlike yourself that are less familiar with Rails templating, let me say that ViewComponent supports any and all templating engines that Rails supports... or more specifically, any templating engine that has been registered with ActionView. ViewComponent uses At first glance, I like BRB. It sort of reminds me of the old DOS command prompt.
I'd be happy to pair on the |
Beta Was this translation helpful? Give feedback.
-
Thank you for getting back to me! It took me a bit to get back to this too 😄 I just threw my hacky regex based implementation up at https://github.com/kaspth/brb so it's a little easier to check out and try. And yeah, I didn't mean to imply that ViewComponent should add specific support for BRB — with the gem above it should already work out of the box! It was more if there was shared interest around the syntax and if people were interested in collaborating, since ERB is the templating default for ViewComponent too.
This is also why I'm asking for help if anybody likes the proposed syntax and wants to pair, because I can't figure this out on my own 🙂
I'd love this, yeah! 😄❤️ |
Beta Was this translation helpful? Give feedback.
-
Cool, will take a look!
Awesome, just private-mentioned you on Mastodon. |
Beta Was this translation helpful? Give feedback.
Hey @kaspth, thanks for bringing BRB to our attention, and apologies it's taken a long time for a maintainer to respond!
I assume you created this discussion with the goal of hearing from the ViewComponent community, and not to ask if BRB support could be added to the framework. For those unlike yourself that are less familiar with Rails templating, let me say that ViewComponent supports any and all templating engines that Rails supports... or more specifically, any templating engine that has been registered with ActionView. ViewComponent uses
ActionView::Template.handler_for_extension
to get a handler instance.At first glance, I like BRB. It sort of reminds me of the old DOS command pro…