3 ms·
I LOVE tagged templates in JavaScript. But in Python I could also imagine YET ANOTHER constant prefix, like t"", that returns a "template" object of some sort,
by ianbicking 2y ago
I LOVE tagged templates in JavaScript.
But in Python I could also imagine YET ANOTHER constant prefix, like t"", that returns a "template" object of some sort, and then you could do html(t"") or whatever. That is, it would be just like f"" but return an object and not a string so you could get at the underlying values. Like in JavaScript the ability to see the original backslash escaping would be nice, and as an improvement over JavaScript the ability to see the expressions as text would also be nice.
But the deferred evaluation seems iffy to me. Like I can see all the cool things one might do with it, but it also means you can't understand the evaluation without understanding the tag implementation.
Also I don't think deferred evaluation is enough to make this an opportunity for a "full" DSL. Something like a for loop requires introducing new variables local to the template/language, and that's really beyond what this should be, or what deferred evaluation would allow.
- pauleveritt 2y agoCool to see you jump in, Ian. I don't particularly mind the prefix thing. It came up in the PEP discussion, as did choice of backticks to indicate this is different. But JS template literals -> tagged template literals shows, you can get from A to B without a fundamental change. I'm very interested though in the deferred part. I agree that there is complexity. I weigh that, though, against the complexity of existing Python HTML templating, where finding out what just happened is...harder. I think we can get a TSX-level of DX out of this. And maybe a Lit level of composition. Agree that it is non-zero complexity.
- ianbicking 2y agoHey Paul! I think JSX is an example of the somewhat crude but practical use of simple execution patterns. For instance if you have a loop you do: return <ol> {items.map((item, i) => <li key={i}>{item}</li>)} </ol>; Which isn't really templating at all, but just the ability to use inline expressions and easily construct objects does get the job done. Or in a SQL builder with JavaScript tagged templates, I do: exec(sql` SELECT * FROM a_table WHERE category = ${category} ${subcategory ? sql`AND subcategory=${subcategory}` : sql``} `) That is, I nest tagged templates to handle different logic conditions and loops. If there's deferred execution, it's done with ?: and .map() – though these very long expressions don't work nearly as well in Python. (List comprehension is in some ways better than .map()/.filter(), but not for very large expressions like in a JSX template.)