# HTML templating a la Mustache?

**URL:** <https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631>\
**Category:** Ecosystem\
**Tags:** web\
**Created:** [July 17, 2023, 12:45pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631 "2023-07-17T12:45:34Z")\
**Posts on this page:** 7\
**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:** [July 17, 2023, 12:45pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/1 "2023-07-17T12:45:34Z")

</div>

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.

- [Mustache](https://github.com/rgrinberg/ocaml-mustache) - templating
- [Dream](https://github.com/aantron/dream) - web framework, including EML
- [Dream](https://aantron.github.io/dream/#templates) - Dream templates EML
- [Dream-HTML](https://github.com/yawaramin/dream-html) - AST-based HTML generation
- [TyXML](https://ocsigen.org/tyxml/4.4.0/manual/jsx) - not familiar with it
- [Jingo](https://github.com/tategakibunko/jingoo) - inspired by Python

Earlier discussions:

- [Choice of template language for tyxml](https://discuss.ocaml.org/t/choice-of-template-language-for-tyxml/1863)
- [Generating HTML: TyXML vs Mustache](https://discuss.ocaml.org/t/generating-html-tyxml-vs-mustache/7065)
- [Htmx/hc web development approach - #21 by cemerick](https://discuss.ocaml.org/t/htmx-hc-web-development-approach/9993/21) - addendum; very useful

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.

---

<div class="post-metadata">

**Author:** ![EmileTrotignon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emiletrotignon/32/5913_2.png) [@EmileTrotignon](https://discuss.ocaml.org/u/EmileTrotignon)\
**Post date:** [July 17, 2023, 1:21pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/2 "2023-07-17T13:21:28Z")

</div>

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](https://ejs.co/) 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.

---

<div class="post-metadata">

**Author:** ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)\
**Post date:** [July 17, 2023, 2:27pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/3 "2023-07-17T14:27:32Z")

</div>

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](https://discuss.ocaml.org/t/htmx-hc-web-development-approach/9993/16), and a pile of replies further downthread.

---

<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:** [July 17, 2023, 2:27pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/4 "2023-07-17T14:27:51Z")

</div>

Most of my experiences with [jingoo](https://ocaml.org/p/jingoo/latest) have been pretty pleasant. It’s basically the same thing as jinja2, which means a lot of tooling is compatible.

---

<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:** [July 17, 2023, 2:56pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/5 "2023-07-17T14:56:03Z")

</div>

Thank you for pointing to the [Htmx/hc web development approach - #21 by cemerick](https://discuss.ocaml.org/t/htmx-hc-web-development-approach/9993/21) discussion. It it essentially about the same question and contains useful information about Dream’s EML and how to handle it during development.

---

<div class="post-metadata">

**Author:** ![beajeanm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/beajeanm/32/189_2.png) [@beajeanm](https://discuss.ocaml.org/u/beajeanm)\
**Post date:** [July 21, 2023, 1:25pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/6 "2023-07-21T13:25:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cemerick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cemerick/32/1383_2.png) [@cemerick](https://discuss.ocaml.org/u/cemerick)\
**Post date:** [July 21, 2023, 1:34pm UTC](https://discuss.ocaml.org/t/html-templating-a-la-mustache/12631/7 "2023-07-21T13:34:26Z")

</div>

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. 🤷)
