Generating HTML: TyXML vs Mustache

I’ve developed a tool that generates a report in HTML by using OCaml Mustache and found that working well. Now I’m contemplating turning this into a web service using Opium. I notice the preference of Opium for TyXML and understand the safety and control it provides. However, it is quite a step away from a template based approach and maintaining a lot of HTML inside OCaml source code does not appeal to me. Any experiences?

2 Likes

There’s TyXML JSX syntax. It gives a template like system with a very close to html syntax + some type safety. I find it convenient to use. The possible downside of this solution is that it’s using the reason syntax which might not be here forever. I don’t have a significant experience with mustache to compare.

3 Likes

This will not be immediately useful to you, but it’s worth noting that @Drup had at some point the project of integrating a template engine with tyxml, exactly for the purpose you mention (Choice of template language for tyxml). And some very recent work by @gasche on ocaml-mustache (in particular https://github.com/rgrinberg/ocaml-mustache/pull/58) looks like it could be very useful for that project.

2 Likes

There’s no need to fuss with Reason, right? You can just use TyXML Ppx syntax extension

At times I prefer to “purely” separate raw data and presentation. So I publish domain specific xml plus an xslt to html

e.g. https://demo.mro.name/geohash.cgi/u28br or
https://demo.mro.name/shaarligo or
http://rec.mro.name/stations/b2/now

So there’s very little markup-fiddling in code (which I don’t find worth while a dependency) and all markup stuff is in the browser in xslt - no javascript involved.

Main Downside: Google won’t index neither the resulting html nor the raw data.

I think the main advantage of a template-based approach is that it works well in these situations:

  • design prototyping and turning a design into an application
  • using an existing design framework
  • collaboration between designer and developer

Keeping the entire design in TyXML creates a barrier. I also like that Mustache has a CLI that populates a template from JSON data, which supports prototyping.

1 Like

I just came across this old thread, which you might find interesting: Choice of template language for tyxml

3 Likes

I entirely agree, and I think it’s very important to enable the “traditional” workflow of teams building web applications. That’s why I wanted to have a tool that would take a standard “templated HTML” file, and turn it into an OCaml module using tyxml to build the HTML.

Unfortunately, time is limited and it’s a complicated piece of code to write, so I stopped working on it. I think now all the pieces are there, so it’s time to finally do it. As @armael said, mustache’s recent additions make it a valid templating language for this job, just need to write the tyxml side. :slight_smile:

I think I’ll free some times in the coming month to work on that, but any help is appreciated, either in tyxml directly, or to improve the mustache OCaml implementation! :+1:

7 Likes