miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Html.Render

Description

Overview

Miso.Html.Render provides the ToHtml typeclass for serialising a View tree to a lazy ByteString of UTF-8 HTML. This is the foundation of miso's server-side rendering (SSR) support.

Instances are provided for both View () () () a (a single node) and [View () () () a] (a sequence of nodes): a bare View is static markup with no enclosing component and no app-global context to read, so its context, props and model are all fixed to (). A component's view, or any subtree that reads context / props / model, is rendered with toHtmlWith.

Quick start

import           Miso.Html.Render (ToHtml, toHtml, toHtmlWith)
import qualified Data.ByteString.Lazy as L

renderPage :: Model -> L.ByteString
renderPage m = toHtmlWith () () m (view () () m)

staticPage :: L.ByteString
staticPage = toHtml (div_ [] [ "Hello, world!" ])

With servant, use toHtml inside a ByteString or OctetStream response, or wire it into a ToHtml servant MIME type.

Rendering rules

  • VNode — rendered as <tag attrs>children</tag>. Self-closing elements (<br/>, <img/>, <input/>, …) are rendered without a closing tag.
  • VText — rendered as a raw text string (no escaping beyond what is already in the MisoString).
  • VComp — recursively renders the sub-component's view using its initial (or hydrated) model.
  • VFrag — renders all children inline, no wrapper tag.
  • VContext (ambient accessor, not a node) — applied to the app-global context (() for a bare View under toHtml; the value given to toHtmlWith otherwise) and the result rendered in its place.
  • VProps (ambient accessor, not a node) — applied to the props of the enclosing component (() for a bare View under toHtml; the value given to toHtmlWith otherwise) and the result rendered in its place.
  • VModel (ambient accessor, not a node) — applied to the initial (or hydrated) model of the enclosing component (() for a bare View under toHtml; the value given to toHtmlWith otherwise) and the result rendered in its place.
  • Event handlers (On) — silently dropped; they have no meaning in a static HTML string.
  • Boolean properties (disabled, checked, required, …) — rendered as bare attribute names when True, omitted entirely when False.
  • Adjacent text nodes — collapsed into a single text node to match browser parsing behaviour during hydration.

SSR flag

When compiled with -fssr the renderer calls the component's optional hydrateModel action to derive the initial model (e.g. by fetching from a database), falling back to the static model if the action throws.

See also

Synopsis

Classes

class ToHtml a where Source #

Class for rendering HTML

Methods

toHtml :: a -> ByteString Source #

Instances

Instances details
(context ~ (), props ~ (), model ~ ()) => ToHtml [View context props model action] Source #

Render a [Miso.Types.View] to a L.ByteString. Adjacent text nodes are collapsed across the list, as they are among an element's children.

Instance details

Defined in Miso.Html.Render

Methods

toHtml :: [View context props model action] -> ByteString Source #

(context ~ (), props ~ (), model ~ ()) => ToHtml (View context props model action) Source #

Render a Miso.Types.View to a L.ByteString

Rendering never starts the runtime, so a bare View has no component to supply its context or props; both are fixed to () (the equality constraints let a View that is polymorphic in either still resolve this instance). A VContext or VProps at this level therefore sees (); one nested inside a mounted component sees that component's real props (and the same context). Use toHtmlWith to supply real values.

Instance details

Defined in Miso.Html.Render

Methods

toHtml :: View context props model action -> ByteString Source #

Functions

toHtmlWith :: context -> props -> model -> View context props model action -> ByteString Source #

Render a View to a L.ByteString, supplying the app-global context, the props and the model that VContext / VProps / VModel accessors in it resolve against. Mounted child components see the same context, and their own props and initial model.

This is the general form of toHtml, for a View whose context, props or model type is not () — e.g. a component's view applied directly. The view itself takes only the model; the context and props given here are what its VContext / VProps accessors resolve against:

toHtmlWith ctx props model (view comp model)

Since: 1.14.0.0