HTML templating a la Mustache?

I’m looking for recommendations to generate HTML by inserting values into a template. I’m currently using Mustache and plan to use it inside Dream. Mustache takes values as a JSON dictionary and can refer to them inside the template. Dream has its own solution EML and I’m not sure about switching. Maybe there are other good solutions. I am not interested in methods that create the entire document as AST using OCaml because it would requite to encode existing templates in OCaml first.

Earlier discussions:

The appeal of templating is that you can work with HTML-aware tools on HTML rather than working in an OCaml environment that is HTML unaware.

1 Like

I think having an html langage server with eml should work at least a little bit.
If it does not, I would be interested to know why. To enable your html mode, you can name your template file foo.eml.html
EML was based on ejs which is reasonably established, maybe you could use their tools to get the html-support.

I think tyxml is html-based, but I also think that there is a ppx that allows you to use it as you would a text-based templating engine. I am not a 100% sure about that, but it would allow you to reuse your existing templates.

1 Like

My personal recommendation is tyxml via its jsx ppx. This allows you to write your templates in .re files, and thus use a very natural HTML syntax, interspersing OCaml values as needed using the tyxml APIs for those times when simple interpolation of values isn’t sufficient. There was some recent-ish discussion around this topic, e.g. Htmx/hc web development approach - #16 by cemerick, and a pile of replies further downthread.

2 Likes

Most of my experiences with jingoo have been pretty pleasant. It’s basically the same thing as jinja2, which means a lot of tooling is compatible.

2 Likes

Thank you for pointing to the Htmx/hc web development approach - #21 by cemerick discussion. It it essentially about the same question and contains useful information about Dream’s EML and how to handle it during development.

What’s the editor support for this workflow?
One thing I like about a pure templating approach is I can treat my mustache files (or similar) has HTML and get full lsp support for that.

1 Like

The OCaml Platform extension for vscode handles this workflow beautifully, which is a big reason why I chose it amongst the alternatives. It has “native” support for editing Reason files, so you can write HTML, interspersed logic for preparing tyxml values as necessary to be spliced into that HTML, definitions of helper functions and modules (using Reason syntax), and things like code completion, goto definition, etc all work as they do in OCaml sources.

(Parenthetically, I will say that it feels like the extension (or the lsp server, not sure) has to do more work when editing Reason files, or perhaps there is some space leak in that side of things; editing Reason files isn’t quite as snappy as when one is editing OCaml sources, something that gets progressively pronounced after some hours of aggregate work with a lot of Reason. A quick restart of the lsp server always seems to resolve it. :person_shrugging:)

2 Likes