3 ms·
Whenever I encounter a template language with expressions reaching into some view model or worse, I come back to an old idea of mine: what if you strictly wrote
by usrusr 3y ago
Whenever I encounter a template language with expressions reaching into some view model or worse, I come back to an old idea of mine: what if you strictly wrote template first? The templates come up with names, types for the data identified by the names and nesting/optionally/repetition relationships, and the build process generates an interface for the programming language side to implement. Interface as in java interface, and curiously also as in typescript interface, despite the huge difference. A template-first viewmodel, including composability where a type is a reference to some other template.
Does this exist somewhere? My impression is that the tedious use of half-baked expression language to pull content from the (typed or not) programming language world into the template has always remained not quite enough of a pain point to drive this reversal and the rise of approaches that don't use separate templates at all (react et al) has made interface-from-template even less likely to happen.
- tubthumper8 3y agoInteresting idea! As an example, if the template was "<span>{name}</span>" then if I follow you correctly, it would generate some `Model` struct/interface/whatever that would have a `name: String` field. I think a problem comes about when it's instead: "<span>{itemCount}</span>" Obviously it can't know that this intends to be an integer. Would it generate an `itemCount` field that is something generic like "impl Display"? Or still String? Either way, you may have other logic that needs this `itemCount` as an integer, for arithmetic or something. Perhaps two models, both auto-generated for all String fields but one you don't touch and the other you can change fields as long as they're convertable to String. I think the main problem is going from a more structured to less structured (model to template) is possible but going the reverse way (template to model) can't always infer the structure.
- jitl 3y agoWell, you’d need to express the types of the arguments your template expects, and then your template type system and your host language type system need to agree on those types, and now you need to code-gen the host language struct type from the template args type - but what’s the point? It’s still needless toil in my view. Just write a function in the host language, it already has a great way of expressing the types of arguments passed to a function (hopefully)
- usrusr 3y agoTemplates would of course have to define types, at least where string wouldn't be considered good enough. Also names for repetitions and optional blocks. It would not solve any big problems when you start with a detailed plan and everything is going according to it. Perhaps that's the reason why fragile expression languages are so often considered good enough: it cements the importance of the person writing the spec (sorry for the sarcasm)