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

Contents

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 m a (a single node) and [View m a] (a sequence of nodes).

Quick start

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

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

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.
  • 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
ToHtml [View m a] Source #

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

Instance details

Defined in Miso.Html.Render

Methods

toHtml :: [View m a] -> ByteString Source #

ToHtml (View m a) Source #

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

Instance details

Defined in Miso.Html.Render

Methods

toHtml :: View m a -> ByteString Source #