Thanks, confusion about the application and the intended use cases seems to be a common issue among other comments.
Initially thought it was an addon (or JS lib) that allowed for using Ruby as an embedded client-side scripting language that would parse in-line Ruby in the HTML.
Clearer once I realized it was closer to PHP or similar language usages.
Second also to the recommendation about a quick definition of ERB (Embedded RuBy). Had to look it up.
A few practical examples of what an actual user might want to do with HTML + ERB could be helpful.
- Client characteristics customization
- Previous use history customization
- The database Create, Read, Update, and Delete cycle using Herb
- A basic Content Management System example
- A basic e-commerce example
- A basic RESTful API example
Note: These are just my guesses based on what I "believe" its for.
ERB is a server-side templating language. It's akin to PHP, Jinja for Python, JSP (Java), ASP.NET Web Pages, etc.
What these do is let you do things like turn a collection of objects into a the HTML for a table and send that over the wire. The client then just has time to display the table and not assemble it from e.g. JSON. ERB and similar allow you to build dynamic sites without using any Javascript at all.
To a first-order approximation, any dynamic site that does not use React or Angular will use a templating language like ERB.
If it is what I think it is, it’s extremely neat. Parsing both HTML and templating syntax at the same time is way more robust than doing just the templating and hoping for the best.
I was thinking about something like this, but with some blend of Jinja2 / Twig / Nunjucks [1] syntax and Svelte/JSX-like use of variables in element attributes:
{% for para in page.body %}
<p class={{ para.class }} {{ **para.attrs }}>
{{ para.text }}
</p>
{% endfor %}
---
[1]: the irony is not lost on me that I’m mentioning Python, PHP and JS template engines in a Ruby discussion :-) Liquid is the closest equivalent I think, but there was some crucial piece missing last time I had to use it, though I can’t remember what exactly.
I was afraid at first this is the only option (which makes it difficult to generalize this to loops with body consisting of anything but a single element). But as an optional shorthand it is indeed pretty neat! (Personally though, I still like explicit “layering” of a separate `for` block a bit more.)
Yeah, that’s intentional — I think having a clear separation between templates and code is a good thing. Regardless, you can use most of Python syntax in Jinja2 (and in my interpretation too, of course).
One thing that's not very clear from the documentation is what the application of this may be. I see that it parses ERB and or HTML into an AST. Is this intended to enable improved HTML and ERB linters and language servers? This seems like a tool that other development tools will benefit from.
I realize this is new, but as tools start using herb, it would nice to link to those tools from the herb website and or github readme.
If you're not familiar with the Ruby ecosystem, the site does a very poor job of communicating what this is actually about. A single definition of ERB would have helped a lot.
I’m not sure it’s targeted towards those not familiar with Ruby... though yeah, at least mentioning it’s a templating engine would make it a lot easier to grasp.
(ERB is, I think, “embedded Ruby”, and it functions basically like old school PHP: you just insert pieces of Ruby code wrapped by <% .. %> or <%= .. %>: https://github.com/ruby/erb#recognized-tags)
ERB is also the default templating language of Rails. I wish Django would use an embedded Python instead of the mutilated tool it uses in its own templates. Not only it's yet another language to learn, but it's also underpowered and it forces developers to define custom tags and filters where a call to a method of a plain Python object would be enough. Apparently they are afraid that people embed logic in the templates but I've seen db calls inside the Python code of the custom tags. There is no perfect defense.
Initially thought it was an addon (or JS lib) that allowed for using Ruby as an embedded client-side scripting language that would parse in-line Ruby in the HTML.
Clearer once I realized it was closer to PHP or similar language usages.
Second also to the recommendation about a quick definition of ERB (Embedded RuBy). Had to look it up.
A few practical examples of what an actual user might want to do with HTML + ERB could be helpful.
Note: These are just my guesses based on what I "believe" its for.What these do is let you do things like turn a collection of objects into a the HTML for a table and send that over the wire. The client then just has time to display the table and not assemble it from e.g. JSON. ERB and similar allow you to build dynamic sites without using any Javascript at all.
To a first-order approximation, any dynamic site that does not use React or Angular will use a templating language like ERB.
Wow!!! Cool to see ERB being embraced and enhanced in this way, I’ll have to check it out
I was thinking about something like this, but with some blend of Jinja2 / Twig / Nunjucks [1] syntax and Svelte/JSX-like use of variables in element attributes:
---[1]: the irony is not lost on me that I’m mentioning Python, PHP and JS template engines in a Ruby discussion :-) Liquid is the closest equivalent I think, but there was some crucial piece missing last time I had to use it, though I can’t remember what exactly.
It works pretty seamlessly and you get something like this:
Note, I'm not using FastHTML's built-in database interface. I prefer a little less implicit than the strict FastHTML style.Template engines like liquid are good for simpler use cases where you'd want your end users to write templates (who aren't programmers)
And no, it’s not only good for simple use cases. Template inheritnace alone is a killer feature for bigger projects: https://jinja.palletsprojects.com/en/stable/templates/#templ...
I realize this is new, but as tools start using herb, it would nice to link to those tools from the herb website and or github readme.
(ERB is, I think, “embedded Ruby”, and it functions basically like old school PHP: you just insert pieces of Ruby code wrapped by <% .. %> or <%= .. %>: https://github.com/ruby/erb#recognized-tags)