One Model...

One Model...

I created a web site for my and clergy colleagues' use (legereme.com) . There is nothing special about it except that it works and it's written using Elixir, Phoenix, and Elm (which is really cool).

Now, I have a couple of colleagues generating content for it. My colleagues are brilliant and insightful people but frequently struggle with 20th [sic] century technology. They are just not computer people (how brilliant and insightful people can not be technologically insightful and brilliant is beyond me).

I want them to generate their content using markdown formatting as I think it is within their grasp and makes my life easier when it comes to publishing their content. Even so, they grew up with WYSIWYG word processors and the concept of data being separate from presentation is, perhaps, just at the limit of their technological reach.

First I considered having a <preview> button, but decided more immediate feedback would be desirable.

Elm to the rescue. And as it turned out, easier to implement than a <preview> button.

The content creation page is very simple with a couple of buttons, a textarea, and preview area. The user enters markdown into the textarea and simultaneously appears formatted in the preview area. Simple. And unbelievably easy to implement in Elm.

The page has a single model and changes to the model cascade through the page. The user enters a character in the textarea...

inputText model =
  p [ id "refl-textarea"]
    [ textarea
      [ id "reflection-text"
      , name "reflection-text"
      , placeholder "Use Markdown formatting"
      , Html.Attributes.value model.text
      , onInput ReflectionText
      , autofocus True
    ]
    []
]

The relevant bit is "onInput ReflectionText". When the text in the textarea changes, the single model is updated. That code looks like...

ReflectionText s -> ( { model | text = s }, Cmd.none)

"s" is the text in the textarea, and the "text" element in the model is updated. When the model is updated, Elm handles the bit of seeing if anything else on the page needs to be updated. As it turns out, the preview area displays the text element of the model, presuming it is in markdown format...

div [id "md-text"] [Markdown.toHtml [] model.text]

And that's all there is to it.

There are two bits of Elm brilliance at play here. The first being; there is a single model. Admittedly, this was a bit hard for me to wrap my head around at first, but having accepted the "Elm way" I can see the superiority of using a single model. But that's just a technique, a worthy technique. 

The second bit of Elm brilliance is Elm watching for changes in the model and updating anything that depends on that model. Brilliant, just brilliant.

To view or add a comment, sign in

More articles by Paul Sutcliffe

  • Measuring Credibility: It can be done.

    I have been troubled by how language is used in our day and age, noticing that dubious claims are made louder and more…

    1 Comment
  • Validating Claims in a Post-Truth World: Why Liars Shout and Prophets Whisper

    In seminary, when we studied preaching, there was a story passed around, no doubt apocryphal, about a preacher who…

    1 Comment
  • Vibe Coding

    Vibe Coding I am working on an app for clergy, and I have been using claude.ai to help.

    1 Comment
  • 263 Vanadians (What's with 153 fish, anyway?)

    May 4, 2025 To the Saints at Vanadium Woods and beyond, May 4, 2025 Grace and peace from Jesus Christ our Lord, who…

  • 262 Vanadians (In praise of Doubting Thomas)

    To the Saints at Vanadium Woods and beyond, April 27, 2025 Grace and peace from Jesus Christ our Lord, who tells us the…

  • Books Finished!

    Words are hard. I am happy to report that after two and a half years, I have FINALLY pushed two books out the door to…

  • 1 Vanadians

    To the Saints of Vanadium Woods Village Grace and peace from our Lord Jesus Christ and from Fr. Paul who is stuck at…

    1 Comment
  • More Elm

    My second Elm web app is well into beta now and would be spectacularly uninteresting to most people as it's sort of a…

    1 Comment
  • Elixir + Phoenix + Elm Project

    I've been working on this open source donor database for non-profits for a couple of months now and it's far enough…

    2 Comments
  • Elixir/Phoenix/Elm - it's worth the effort

    The following was written when Elm was in it's pre-1.0 state.

Explore content categories