Live data from Hacker News

Foolproof HTML

pumpula.net

11–20 of 110 posts

Re: Foolproof HTML

#12

I found my foolproof HTML in slim-lang. It produces standards-compliant HTML and prevents me from writing code that is not well-formed. The above is a nice side effect of its incredibly clean and terse syntax. Now I feel cheated any time I need to write regular HTML. https://github.com/slim-template/slim/blob/master/README.md I used emmet for a while but slim improves on writing and reading code.

Slim becomes painful when using Ruby function calls (i.e. Rails helpers). Sometimes these functions have long list of parameters and wrapping to next line sometimes becomes not so convenient. Moving, copying and pasting indented blocks is also quite painful and it's much harder to see nested structure than in, for example, Python code. Also it has complicated syntax for attributes and text content, especially text content. I have to look up its docs again and again. It reminds me of yaml which has the same problems (but much worse, yaml's documentation has size of a book).

However I'm not a fan of ERB or Django/Jinja template markups either. Indentation-based syntax has its advantages, it works well for Python and ML-like languages. Just Slim can be improved further.

Re: Foolproof HTML

#14

I found my foolproof HTML in slim-lang. It produces standards-compliant HTML and prevents me from writing code that is not well-formed. The above is a nice side effect of its incredibly clean and terse syntax. Now I feel cheated any time I need to write regular HTML. https://github.com/slim-template/slim/blob/master/README.md I used emmet for a while but slim improves on writing and reading code.

Seems similar to HTML generation libs in Lisp, except with indentation instead of parenthesis.

Re: Foolproof HTML

#15
One of the things I like about JSX is that it will always make sure you write valid syntax.

Problem with things like slim is that you cannot copy-paste directly from HTML.

Re: Foolproof HTML

#17
> If you have a good strategy for validating your template files, I'd love to hear it!

Use S-expression syntax instead of SGML syntax, i.e. instead of:

content

write

((tag attr value ...) content)

and use Lisp to process it. It's actually quite straightforward. You can apply it to XML as well. Everything actually ends up looking a lot prettier this way.

See http://weitz.de/cl-who/ for an example of an implemented system that works this way. I've been using it in production for years. It works like a charm.

Re: Foolproof HTML

#19
Interesting. Despite the many approaches to writing HTML more are invented all the time. Currently I'm working on yet another way. My idea follows the Lisp tradition based on its hierarchical list structures. It's surprisingly easy to parse and generate HTML from basically simple lists.

    '(html
      (head
        head stuff ...)
      (body
        (h1 "My HTML")
        (div (@ class "main-div" ...)
          "main div stuff")))
Of course it gets a lot more complicated when generating complete web pages and apps, but using paste operators, procedures and variables can make it produce anything necessary.

A very similar approach is possible with Tcl, which I am using now for several reasons including integration with legacy code. In Tcl the above looks like:

    {html
      {head
        head stuff ...}
      {body
        {h1 {! My HTML}}
        {div {@ class "main-div" ...}
          {! main div stuff}}}
In Tcl variables and procedures can be interpolated into the output by wrapping the list in a "subst" command:

    [subst {html .... [my-proc $my_var] ....}]
The hierarchical nature of Lisp-like languages naturally resembles the GUI outline form the author presents in the article. Using Tcl it's occurred to me that it should be possible to write a "front end" to the parser/generator code using Tk (via the text or canvas widgets) which could resemble the author's illustrations.

Ultimately I think the difficulty lies in the need for constructing customized templates to enable modular, easy-to-use GUIs for particular tasks. Real HTML can quickly become very complex which is the reason for the thousands of frameworks, generators and templating systems in existence. While I think the author's approach or my own can potentially be helpful, neither is going to magically eliminate the burden on the programmer.

Re: Foolproof HTML

#20
post #17

> If you have a good strategy for validating your template files, I'd love to hear it! Use S-expression syntax instead of SGML syntax, i.e. instead of: content write ((tag attr value ...) content) and use Lisp to process it. It's actually quite straightforward. You can apply it to XML as well. Everything actually ends up looking a lot prettier this way. See http://weitz.de/cl-who/ for an example of an implemented sys…

Can confirm: Hiccup is really great.
Post reply on HN