HTML templates & recursion - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Sridhar Ratnakumar

Is anyone aware of any HTML template library that supports recursion (eg: to render a tree)? While also supporting scoped variable bindings.

Just learned that Heist does: https://github.com/srid/emabook/pull/7/files

Rewrite the hacky sidebar template to use a general "tree splice" that recurses when rendering the sub trees. Use <apply> to recursively invoke the self template.
Mats Rauhala

Admittedly not a real templating library, but can be used for it: dhall

TheMatten

Haha, I've recently used Dhall to quickly put together small static website and it worked pretty well - using little script that called dhall text on directory with .dhall files with structure matching final routes.

Mats Rauhala

My biggest gripe on using dhall for this, is the lack of text operations, not possible to do html escaping

Georgi Lyubenov // googleson78

I also used dhall as a "templating library" once (not for html), but having to write out all the tyvars is annoying :(

TheMatten

Mats Rauhala said:

My biggest gripe on using dhall for this, is the lack of text operations, not possible to do html escaping

Yeah - should be easy enough to extend it using HS API if we were to write templating engine based on it though

Mats Rauhala

Yeah, I went through the XML source a while ago. There's no escaping there either :)

Mats Rauhala
let XML =
      https://prelude.dhall-lang.org/XML/package.dhall sha256:137e7b106b2e9743970e5d37b21a165f2e40f56ab593a4dd10605c9acd686fc6

in  XML.render
      ( XML.element
          { name = "foo"
          , attributes = XML.emptyAttributes
          , content =
            [ XML.leaf { name = "bar", attributes = XML.emptyAttributes }
            , XML.text "asd <asd />"
            ]
          }
      )
Mats Rauhala

Practical concerns aside; It's cool that the XML stuff is based on a recursive tree.

mmhat

Doesn't Dhalls Text/replace builtin work for that? Like Text/replace "<" "&lt;"?

Sridhar Ratnakumar

Make XSLT Great Again (but in Haskell)

Mats Rauhala

Oh, right, https://discourse.dhall-lang.org/t/expense-proposal-standardize-text-replace-built-in/339/2 this was a thing

Should this get approved or not I’d be up for volunteering to try this. Haven’t standardised anything before, but I guess it’d involve updating similar files to say this PR https://github.com/dhall-lang/dhall-lang/pull/1062, and maybe the type inference docs/tests also? If anyone else wants to jump in that’s cool, but thought I’d throw my name in the hat all the same.
Sridhar Ratnakumar

Well, obviously I'm not going to be switching to Dhall because it is rather primitive for my use case of HTML templates (though I'd love to see if Dhall gets first-class HTML templating support and if so how it will manage to deal with XML).

Heist is, surprisingly, plenty powerful. This sidebar tree with node-specific state (open, close, active) https://haskell-kb.srid.ca/Syntax/case-of - is generated by this recursive template

WIP: Spiritual successor to neuron, based on Ema. Contribute to srid/emabook development by creating an account on GitHub.
Sridhar Ratnakumar

And here's the corresponding Haskell code. See how polymorphic it is over the tree type. You can use any Data.Tree. https://github.com/srid/emabook/blob/84ee208d174b8f751e98725cd63bb16771597bad/src/Heist/Extra/Splices/Tree.hs#L15-L38

WIP: Spiritual successor to neuron, based on Ema. Contribute to srid/emabook development by creating an account on GitHub.
Mats Rauhala

https://github.com/dhall-lang/dhall-lang/pull/1174 interesting. The xml escaping was not only a known issue, but something that was fixed in the latest release (today)

We need to be escaping < in as < in XML text (I disovered this while writing an Atom feed generator).