# Dream: ppx vs external program

**URL:** <https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695>\
**Category:** Learning\
**Created:** [March 18, 2023, 4:51am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695 "2023-03-18T04:51:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 18, 2023, 4:51am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/1 "2023-03-18T04:51:08Z")

</div>

In this example here: [dream/dune at master · aantron/dream · GitHub](https://github.com/aantron/dream/blob/master/example/7-template/dune#L5-L8)

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](https://github.com/aantron/dream/blob/master/example/7-template/template.eml.ml#L1-L6)

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

---

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [March 18, 2023, 5:13am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/2 "2023-03-18T05:13:29Z")

</div>

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](https://ocaml.org/docs/metaprogramming#ppxs). Specifically, `<html>` and `<body>` do not seem like valid OCaml syntax.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [March 18, 2023, 5:40am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/3 "2023-03-18T05:40:18Z")

</div>

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](https://github.com/aantron/dream/issues/156)

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 18, 2023, 6:39am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/4 "2023-03-18T06:39:35Z")

</div>

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](https://github.com/ocsigen/tyxml/blob/master/examples/basic_website/site_html.ml) .

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [March 18, 2023, 6:40am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/5 "2023-03-18T06:40:49Z")

</div>

> [@jbeckford](#):
>
> My guess is that `.eml` files cannot be expressed on top of the OCaml AST.

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](https://ocsigen.org/tyxml/latest/manual/ppx)

```ocaml
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)

---

<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:** [March 18, 2023, 6:53am UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/6 "2023-03-18T06:53:51Z")

</div>

> [@zeroexcuses](#):
>
> 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](https://github.com/ocsigen/tyxml/blob/master/examples/basic_website/site_html.ml) .

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

---

<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:** [March 18, 2023, 4:47pm UTC](https://discuss.ocaml.org/t/dream-ppx-vs-external-program/11695/7 "2023-03-18T16:47:16Z")

</div>

> [@zeroexcuses](#):
>
> 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](https://github.com/ocsigen/tyxml/blob/master/examples/basic_website/site_html.ml) .

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