2 ms·
> You really should use a tool that respect the semantics of the language. What tool are you referring to? If one does not exist, why is that?
by mattacular 6y ago
> You really should use a tool that respect the semantics of the language.
What tool are you referring to? If one does not exist, why is that?
- TeMPOraL 6y agoHTML generators that work on trees. They do exist, and they're very popular in Lisp world, though they also exist elsewhere. So instead of reading a template file and interpolating it with strings, you'd write something like (using JS / PHP syntax): const html = ['form', {action: myHandlerUrl, method: 'post'}, ['input', {id: 'key', name: 'key', type: 'hidden', value: randomKey()}], ['button', {type: 'submit'}, "Send!"]]; OutputHtml(html); With OutputHtml() being the replacement for templating engine. The above structure is semantically a tree, and you can't in any way screw it up as to leave tags incorrectly nested (it won't compile/interpret). It's composable too - you can concatenate a bunch of such representations and always get a result that represents a valid tree. OutputHtml() is relatively easy to write even by yourself, because at any given time, when turning a dynamic value into output HTML, it's apparent how the value has to be converted (and/or "escaped"). SQL also has its equivalents of this idea. The most basic of which is "prepared statements" - an API in which, instead of: query("SELECT foo, bar FROM baz WHERE bar = " + userInput " AND foo > " + anotherInput); you write: query(build("SELECT foo, bar FROM baz WHERE bar = ? AND foo > ?"), userInput, anotherInput) which against gives the API the most basic ability to recognize and encode interpolated values correctly. There are of course more complex libraries available, which let you parametrize any part of an SQL query, while making the generation template respect SQL's semantics, and that use composable representations.