Posted by todsacerdoti 3 days ago
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.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.
> 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.
Typo aside, I did try to capture the essential difference of a server-side templating engine vs. how many people build using "modern" web stacks.
But I have worked with enough engineers schooled since React became popular to know that the "modern" Web stack basically is SPAs everywhere.
Incidentally, this same evolution has made SQL something of a dark art among programmers. It's good that the field is big and diverse and specialized! But also, there are tons of professional full-stack Web programmers who are functionally illiterate in SQL due to primarily using NoSQLs or ORMs.
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:
{% 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.
It works pretty seamlessly and you get something like this:
@app.post('/part/add')
def create_part(part_number: str, name: str, description: str):
p = Part(part_number=part_number, name=name, description=description)
p.save()
return (p,
Input(placeholder="part number", id="part_number", hx_swap_oob='true'),
Input(placeholder="description", id="description", hx_swap_oob='true'),
Input(placeholder="name", id="name", hx_swap_oob='true'))
Note, I'm not using FastHTML's built-in database interface. I prefer a little less implicit than the strict FastHTML style. <p :for={para <- paras} class={para.class} {para.attrs}>
{para.text}
</p>
With :for being a list comprehension.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)
Oh, wait...