3 ms·
I love this approach. I just wish there was better tooling for HTML polyglot programming. PHP got it right by embedding itself within "HTML" documents. React a
by pphysch 2y ago
I love this approach. I just wish there was better tooling for HTML polyglot programming.
PHP got it right by embedding itself within "HTML" documents. React also got it right by inventing JSX. There's Go templ too. They all said "HTML deserves first class support".
Why can't this DX also be available for other general purpose PLs? I want my Python language server to detect that this or that string is HTML, and provide features accordingly. I don't want to keep bouncing between .py and .html files. Is this achievable?
- solardev 2y agoIs this kinda what you want? https://peps.python.org/pep-0750/ https://peps.python.org/pep-0750/ (similar to template literals in JS) You can also do something similar using f-strings, at the risk of content injection attacks.
- davepeck 2y agoSlowly but surely... PEP 750 (still a proposal) adds "t-strings" Python. They're a simple extension of f-strings that give developers access to the static and interpolated parts of the string before they get combined into a final string. They're Python's answer to JavaScript template literals. This allows writing code like: user_name = current_user.user_name # something user-supplied and potentially unsafe foo: HTMLElement = html(t"<div>{user_name}</div>") # wouldn't be safe with f-strings where `html` is a normal function that takes a `Template` as an instance, and can return whatever it likes -- say, an `HTMLElement` rather than a `str`. If PEP 750 does get accepted, eventually one hopes the community will introduce formatting, type checking, and LSP smarts for common kinds of t-strings, like HTML and maybe SQL, etc.
- pphysch 2y agoGreat! Hope the proposal gets support.
- freedomben 2y agoI think PHP was brilliant, and to this day is still an underrated model, but as websites grow in complexity, particularly around data and access controls, The model needs to be inverted. When logic gets sprinkled throughout templates, it can turn into an unreadable nightmare. Personally, I think ERB in Ruby with Sinatra or rails really nailed The approach.