Dream: ppx vs external program

In this example here: dream/dune at master · aantron/dream · GitHub

In this example here, what is the rationale of requiring an external processor instead of doing this directly through ppx ?

I suspect it has something to do with dream/template.eml.ml at master · aantron/dream · GitHub

What I don’t understand is: why is this not done via ppx ?

My guess is that .eml files cannot be expressed on top of the OCaml AST. The OCaml compiler has to be able to parse the file before a PPX can process it; see Preprocessors and PPXs · OCaml Tutorials. Specifically, <html> and <body> do not seem like valid OCaml syntax.

3 Likes

There are already two other templating approaches available in OCaml: TyXML DSL, and TyXML JSX (PPX). Dream is using the eml approach to make it a bit simpler for newcomers. There’s more context here: Design Suggestion: Reconsider eml.ml approach · Issue #156 · aantron/dream · GitHub

1 Like

The first comment in the GH issue (on LSP) convinced me to use ppx approach instead of this .eml.ml approach.

I’m familiar with the jsoo-react syntax. Is it possible to use that directly for HTML templates, or do I need to use tyxml dsl ? I prefer jsoo-react to tyxml/site_html.ml at master · ocsigen/tyxml · GitHub .

Strictly speaking that’s not true, the use of .eml just seems to be a particular opinionated choice adopted by the dream developers.

While ppxs do need to be able to parse the file using standard ocaml syntax, you can somewhat escape the system by embedding your language within a string constant to be processed at compile time — the {<name>| ...|<name>} syntax format is a nice convention for highlighting that in the source code; A proper ppx-embedding is provided by ocsigen’s ppx for html (as mentioned by @yawaramin ):

https://ocsigen.org/tyxml/latest/manual/ppx

let content = [%html{|<div id="content">some content</div>|}] ;;
val content : [> Html_types.div ] Html.elt

Personally, I don’t like this style because it means that your editor is unable to provide any support for editing within the embedding code (because it is seen as just a string content), but this same limitation applies to Dream’s eml approach, so it’s not clear what’s being gained here.

Edit: @zeroexcuses , the tyxml-ppx ppx might be what you’re looking for (see above)

1 Like

Yes you should be able to do that, check https://github.com/aantron/dream/tree/master/example/r-tyxml

Yes, I use tyxml-jsx and .re files, but strictly for HTML templating – all of my types and “real” logic remains in OCaml syntax, as I fundamentally can’t stand Reason syntax outside of its support for HTML literals. :upside_down_face:

1 Like