# Generating HTML: TyXML vs Mustache

**URL:** <https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065>\
**Category:** Ecosystem\
**Created:** [January 9, 2021, 11:00am UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065 "2021-01-09T11:00:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [January 9, 2021, 11:00am UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/1 "2021-01-09T11:00:57Z")

</div>

I’ve developed a tool that generates a report in HTML by using [OCaml Mustache](https://github.com/rgrinberg/ocaml-mustache) and found that working well. Now I’m contemplating turning this into a web service using [Opium](https://github.com/rgrinberg/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?

---

<div class="post-metadata">

**Author:** ![Khady](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/khady/32/469_2.png) [@Khady](https://discuss.ocaml.org/u/Khady)\
**Post date:** [January 9, 2021, 11:31am UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/2 "2021-01-09T11:31:43Z")

</div>

There’s [TyXML JSX syntax](https://ocsigen.org/tyxml/4.4.0/manual/jsx). 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.

---

<div class="post-metadata">

**Author:** ![Armael](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/armael/32/1223_2.png) [@Armael](https://discuss.ocaml.org/u/Armael)\
**Post date:** [January 9, 2021, 11:58am UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/3 "2021-01-09T11:58:09Z")

</div>

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](https://discuss.ocaml.org/t/choice-of-template-language-for-tyxml/1863)). And some very recent work by @gasche on ocaml-mustache (in particular [https://github.com/rgrinberg/ocaml-mustache/pull/58](https://github.com/rgrinberg/ocaml-mustache/pull/58)) looks like it could be very useful for that project.

---

<div class="post-metadata">

**Author:** ![shonfeder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/shonfeder/32/424_2.png) [@shonfeder](https://discuss.ocaml.org/u/shonfeder)\
**Post date:** [January 9, 2021, 12:39pm UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/4 "2021-01-09T12:39:24Z")

</div>

There’s no need to fuss with Reason, right? You can just use [TyXML Ppx syntax extension](https://ocsigen.org/tyxml/4.4.0/manual/ppx)

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [January 9, 2021, 3:44pm UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/5 "2021-01-09T15:44:32Z")

</div>

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](https://demo.mro.name/geohash.cgi/u28br) or  
[https://demo.mro.name/shaarligo](https://demo.mro.name/shaarligo) or  
[http://rec.mro.name/stations/b2/now](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.

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [January 9, 2021, 5:01pm UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/6 "2021-01-09T17:01:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shonfeder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/shonfeder/32/424_2.png) [@shonfeder](https://discuss.ocaml.org/u/shonfeder)\
**Post date:** [January 10, 2021, 1:33am UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/7 "2021-01-10T01:33:18Z")

</div>

I just came across this old thread, which you might find interesting: [Choice of template language for tyxml](https://discuss.ocaml.org/t/choice-of-template-language-for-tyxml/1863)

---

<div class="post-metadata">

**Author:** ![Drup](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drup/32/35_2.png) [@Drup](https://discuss.ocaml.org/u/Drup)\
**Post date:** [January 10, 2021, 6:50pm UTC](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065/8 "2021-01-10T18:50:51Z")

</div>

> [@lindig](#):
>
> 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

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. 🙂

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! 👍
