by nodesocket · 8y ago · 0 comments
(defun login-panel ()
(pbox "login/register"
(:form :id "logform" :class "nomargin"
(:table :style "border-collapse: collapse"
(:tr (:td :colspan "2" "username:"))
(:tr (:td :colspan "2" (:input :id "loguser" :class "txt" :name "user" :type "text" :size 15)))
(:tr (:td :colspan "2" "password:"))
(:tr (:td :colspan "2" (:input :id "logpass" :class "txt" :name "pass" :type "password" :size 15)))
(:tr (:td :colspan "2" (:input :id "logmem" :type "checkbox" :name "mem" "remember me")))
(:tr (:td :colspan "2" (:span :id "logerror" :class "error" "")))
(:tr (:td (:input :id "logbtn" :class "btn" :type "submit" :value "Login" :onclick "login(); return false"))
(:td (:input :class "btn" :type "submit" :value "Register" :onclick "register(); return false")))
(:tr (:td :nowrap t :colspan "2" :align "center" :class "little" (:a :href "/password" "what's my password?")))))))What you are looking at was designed by a designer.
Maybe you think another designer you know could make something that looks better if only they could use their own tools, instead of tools that this designer liked to use.
I remember an age when I would receive photoshop "designs" that I would have to re-code in HTML. Then they got dreamweaver and thought they didn't need me to code their stuff up anymore, just to debug their stuff and tell them why it didn't work in X/Y/Z browser combination.
When the lisp programmer is writing their login panel, they start out writing "code" like this,
(defun login-panel ()
(pbox "login/register"
then decide they need some HTML. `pbox` recognises HTML as long as it looks like lisp so: (:form :id "logform" :class "nomargin"
(:table :style "border-collapse: collapse"
(:tr (:td :colspan "2" "username:"))
(:tr (:td :colspan "2" (:input :id "loguser" :class "txt" :name "user" :type "text" :size 15)))
(:tr (:td :colspan "2" "password:"))
(:tr (:td :colspan "2" (:input :id "logpass" :class "txt" :name "pass" :type "password" :size 15)))
(:tr (:td :colspan "2" (:input :id "logmem" :type "checkbox" :name "mem" "remember me")))
(:tr (:td :colspan "2" (:span :id "logerror" :class "error" "")))
(:tr (:td (:input :id "logbtn" :class "btn" :type "submit" :value "Login" :onclick "login(); return false"))
(:td (:input :class "btn" :type "submit" :value "Register" :onclick "register(); return false")))
(:tr (:td :nowrap t :colspan "2" :align "center" :class "little" (:a :href "/password" "what's my password?")))))))
can be transformed into the much more complex: <form id="logform" class="nomargin">
<table style="border-collapse: collapse">
<tr><td colspan="2">username:</td></tr>
<tr><td colspan="2"><input id="loguser" class="txt" name="user" type="text" size="15"></td></tr>
<tr><td colspan="2">password:</td></tr>
<tr><td colspan="2"><input id="logpass" class="txt" name="pass" type="password" size="15"></td></tr>
<tr><td colspan="2"><input id="logmem" type="checkbox" name="mem">remember me</td></tr>
<tr><td colspan="2"><span id="logerror" class="error"></span></td></tr>
<tr><td><input id="logbtn" class="btn" type="submit" value="Login" onclick="login(); return false"></td>
<td><input class="btn" type="submit" value="Register" onclick="register(); return false"></td></tr>
<tr><td nowrap="t" colspan="2" align="center" class="little"><a href="/password">what's my password</a></td></tr>
</table>
</form>
But what is the alternative? To embed the html so that it still looks like HTML means making a string of some kind, and running the risk that there's some kind of variable expansion or XSS or other thing that will bite them in the butt. Maybe not in this simple example, but the need to get into the display-language is very common in a web application, so there are lots of fragments like this to answer to:Maybe this one and bigger we could move into a file and either read it every request or add some extra code to save the contents to a global variable. But we haven't gained very much for all that complexity -- this hypothetical "when" we get a designer who knows HTML and not Lisp, and can contribute in a way that meaningfully moves the project forward without making more work for other programmers is far away at this point, and the reddit developers are just going to rewrite the whole thing in Python anyway.
I mean, templates are a thing people use.
I think you implied that in your last point but I'd be curious to find a designer who knew lisp better than HTML.
"Confusing and hard to grok"† is in the eye of the beholder. Syntax highlighting and indentation go a long way (as well as exposure), though even in black and white, the parens fade for me much more than angle brackets do. Personally, I'd wrap some of the longer lines, but that's a style thing.
[0]: https://docs.racket-lang.org/html-template/index.html.
[1]: https://github.com/weavejester/hiccup
[2]: https://mustache.github.io/mustache.5.html
† Sorry. Couldn't let the typo stand. Heinlein made me do it.
RE designer thing, I guess it depends on how you work. Frankly, Reddit to this day doesn't look like anything that has ever met a designer (and that's a good thing in its case). Still, if you want, you can just stick to the designer that does mockups in Photoshop, and skip the other designer that converts those into HTML.
Even without the transpilation step for JSX support, I find hyperscript [0] and friends [1] a notable improvement over other templating tools. Instead of having to jump to a separate file which magically inherits a bunch of implicit globals, you just call a function. If the system supports a component abstraction you can import them and compose views using the same programming constructs you use everywhere else.
An error in the template can be caught by your linter or compiler, since it's just regular code. I'd say it's actually safer, since you typically won't have to worry about escaping values manually. And along the way you can validate the input in order to confirm you only ever generate valid HTML.
Check out elm-css [2] as well, which lets you define your stylesheets with elm. This lets you take advantage of the their type system to make sure all class names are referenced correctly. A typo or accidental deletion would would cause it to fail to compile, allowing you to catch problems early.
[0] https://github.com/hyperhype/hyperscript
That said, glue-strings-together templates are still a problem and should not be used. HTML document is semantically a tree, and if you don't treat it like that, bugs and security vulnerabilities follow.
(Respecting semantics applies to other languages as well. For instance, those who recognized SQL as a language, instead of some blob you concatenated out of strings, didn't have to worry about SQL injection.)
Because if you're working on a team, not everyone on your team may be a programmer, particularly if they're just working on layout design, and the problem that templates solve in that regard don't require complete access to raw code. Separating one from the other makes it easy to focus on one versus the other. Just look at how messy a complex Wordpress template can get and how difficult it can be, just reading it, to get a grasp of what the HTML would look like, versus a Twig template which has a lot less noise.
>Especially that I saw it becoming popular in PHP, which itself is a better templating language than the templating languages people were using.
I've been down the road with PHP where everyone working on javascript and CSS also had to know just enough PHP not to break the site, because everything had raw PHP mixed into it, and then somewhere in the vast tree of includes including includes someone forgot to manually escape a variable that came from the database, or else they did, but now someone else escaped it twice, or they created some weird encoding problem because they were in a javascript context.
PHP is at best an adequate templating language, and a good base to build a sane framework on. But a framework is necessary beyond a certain level of complexity, because most projects benefit from features that raw PHP doesn't provide, such as context-aware and automatic variable escaping, template inheritance, template caching, etc. You will either wind up using an existing templating framework, or you will wind up implementing an ad-hoc, informally-specified, bug-ridden, slow implementation of an existing framework.
>That said, glue-strings-together templates are still a problem and should not be used. HTML document is semantically a tree, and if you don't treat it like that, bugs and security vulnerabilities follow.
There's no way to get that in PHP without a template framework. PHP, unfortunately, doesn't even know that HTML exists[0], despite having the sole purpose of being an HTML preprocessor.
[0]I just remembered there are XML functions like DomDocument that can be used to process HTML (with... effort) but I don't know whether or not you could get something like XHR out of it, and by extension, avoid the problem of HTML as concatenated strings.
I think we're rapidly getting to the point in our civilisation that that's a bit like having an illiterate on the team. All programming is, is thinking logically & systematically about abstractions: everyone should be capable of thinking like a programmer — anyone who can't has a cognitive disability (like those poor folks who can't learn to read).
Fortunately, people who can't think logically & systematically about abstractions are pretty rare; people who won't think logically & systematically are sadly far, far too common.
.. I meant XHP, not XHR, can't edit it now. Oops.
Do you mean that (:td :colspan "2" "username:") is verbose and error-prone compared to <td colspan="2">username:</td>, or what are you comparing it to? That code is probably denser than I would have written it, but I don't see any issue related to the fact the HTML is being generated from Lisp.
This example doesn't take advantage of it, but writing HTML in Lisp is easier and less error-prone than regular HTML, because you can abstract patterns and factor things.
To our parent's points, it's more verbose if you're used to passing data into a separate template file. That can help abstract the markup from the data processing. To their point regarding designers, many are more comfortable working with HTML files than application code. That can be a benefit for many teams. Clearly some lispers agree, as they've implemented alternatives like Mustache templates.
> To our parent's points, it's more verbose if you're used to passing data into a separate template file. That can help abstract the markup from the data processing.
One does not exclude another. The "template" can be another lisp function (in another file).
> Clearly some lispers agree, as they've implemented alternatives like Mustache templates.
Probably popular demand. Personally, I don't think they should be used, like ever. Those template languages were a doubly bad idea.
First, they work on HTML serialization to string instead of HTML semantics as a tree, making it easy to introduce both bugs and security vulnerabilities. The concepts of "escaping", "sanitization" or "injection" are only relevant when you're abusing a serialization format instead of working with the language at the appropriate level.
Second, they all seem to have started with the assumption that "views don't need code". Each of them, over time, slowly discovered that no, views actually do need to have code, and slowly gained variables, conditionals, loops, function calls... and turned into shitty Turing-complete languages they tried so hard not to become. The problem here was always the premise. Views absolutely do need code, and so they should be written in a proper programming language.
For example, imagine if you want to test some HTML code in a browser. Browser doesn't understand Lisp so you have to write it in HTML. And then you have to convert it to Lisp to use as a template. What a waste of time.
There are all king of template languages around. There are editors that can highlight the syntax, there are plugins to write HTML faster. I don't imagine how you justify writing your own template engine. Probably that is just a NIH symdrome.
> First, they work on HTML serialization to string instead of HTML semantics as a tree, making it easy to introduce both bugs and security vulnerabilities.
This can be easily solved by validating HTML code.
> The concepts of "escaping",
Tenmplate engines do necessary escaping automatically. For example, in Twig you just write
<div>{{ user.getName() }}</div>
and don't have to worry about escaping. I guess in Lisp you cannot even write user.getName() because it doesn't have objects.> Views absolutely do need code, and so they should be written in a proper programming language.
In my experience, template engines like Twig have enough features. If you need soem complicated logic, you can write a helper function.
(pbox "login/register"
(form (text "username:" :name "user")
(pass "password:" :name "pass")
(check "remember me" :name "mem")
(group
(button "Login" :onclick "login(); return false")
(button "Register" :onclick "register(); return false"))
(link "what's my password?" :href "/password")))And you can do the same thing with template engines like Twig with macros. Futhermore, Web frameworks like Symfony already have macros that are necessary to display a form. You don't even have to write them and that is a strong point of Symfony compared to weird self-made Lisp template engine.
This thing has no concept of localization, is that a constraint? Accessibility? Are there dozens of other engineers working at this level of detail you need to worry about? Is this the output of a higher level tool used by either coders or designers? Is there a coherent component architecture behind this or is it just more of a utility function someone made? Do components need to be namespaced? Live in their own files/packages? Is the thing you're building a site with several unique pages or a SPA? Where are the JavaScript implementation points for things like the on-click's register() function? Or are you going to forbid JS in your markup (because it confuses designers, or because of CSP policy) and force programmers to bind things elsewhere? How does your routing system work, is it fine to hardcode those paths like that?
That looks a lot less verbose, error-prone, confusing & hard-to-grok than actual HTML, don't you think? Or do you actually prefer the HTML below?
<form id="logform" class="nomargin">
<table style="border-collapse: collapse">
<tr><td colspan="2">username:</td></tr>
<tr><td colspan="2"><input id="loguser" class="txt" name="user" type="text" size="15"/></td></tr>
<tr><td colspan="2">password:</td></tr>
<tr><td colspan="2"><input id="logpass" class="txt" name="pass" type="password" size="15"/></td></tr>
<tr><td colspan="2"><input id="logmem" type="checkbox" name="mem"/>remember me</td></tr>
<tr><td colspan="2"><span id="logerror" class="error"/></td></tr>
<tr><td><input id="logbtn" class="btn" type="submit" value="Login" onclick="login(); return false"/></td>
<td><input class="btn" type="submit" value="Register" onclick="register(); return false"/></td>
</tr>
<tr><td nowrap="true" colspan="2" align="center" class="little"><a href="/password">what's my password?</a></td></tr>
</table>
</form>
For my money, the S-expression HTML is much easier to read & understand.As someone who doesn't, the HTML example is 100% easier to read in my opinion coming from a Rails developer. You don't have to go against the grain to get the output you desire, you can just write it as it is intended to be rendered.
And of course in the context of writing lisp code it means that you have a uniform syntax instead of having some parts of the file as HTML and others in an other language (which is often a pain with code editors in my experience).
It still baffles me that SGML and its descendants are so popular. They're quirky, terribly noisy for markup, incredibly large and slow for serialization and of course every variation introduces and removes arbitrary limitations.
</closingtag> vs )
<form><table></form></table> oops. Can't happen with an s-expressionAnd this can result in sites that are very fast to add things to, as opposed to have dozens and dozens of files everywhere.
But everyone wants "single page apps". Because
client -> server ---[html]--> client --> server --[javascript]--> client --> server --[4*ajax requests necessary to fill in the initial page]--> client --> server --[4*images necessary to fill in said javascript]--> client
Is so obviously "much faster" than: client -> server --[html+embedded everything]--> clientIt's not error prone maybe even less so since it can be checked by the compiler.
My preference is to use templating because it matches the final output closer and is, like you said, easier to show to other people / designers. However, even templating is sometimes turned into an enterprise confusing mess where it becomes very complex and hard to know where values come from.
[1] https://github.com/archimag/cl-closure-template [2] https://developers.google.com/closure/templates/
As far as it being typical... given the zeal that lispers seem to have for s-expressions, yes probably.
[0]https://github.com/wting/hackernews/blob/master/news.arc#L38...
A programmer will translate it to something manageable eventually anyway. A trivial automatic process can convert HTML to a DSL. Although making code sensibly refactored and readable is usually a manual job for the programmer.