| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
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 (a single
node) and View () () () a[ (a sequence of nodes): a bare
View () () () a]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 inside a toHtml
or ByteStringOctetStream 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 theMisoString).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-globalcontext(()for a bareViewundertoHtml; the value given totoHtmlWithotherwise) and the result rendered in its place.VProps(ambient accessor, not a node) — applied to thepropsof the enclosing component (()for a bareViewundertoHtml; the value given totoHtmlWithotherwise) and the result rendered in its place.VModel(ambient accessor, not a node) — applied to the initial (or hydrated)modelof the enclosing component (()for a bareViewundertoHtml; the value given totoHtmlWithotherwise) and the result rendered in its place.- Event handlers (
) — silently dropped; they have no meaning in a static HTML string.On - Boolean properties (
disabled,checked,required, …) — rendered as bare attribute names whenTrue, omitted entirely whenFalse. - 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
- Miso.Hydrate — client-side hydration from server-rendered HTML
- Miso.Html.Element — element smart constructors
- Miso.Html — top-level HTML DSL re-export hub
Synopsis
- class ToHtml a where
- toHtml :: a -> ByteString
- toHtmlWith :: context -> props -> model -> View context props model action -> ByteString
Classes
Class for rendering HTML
Methods
toHtml :: a -> ByteString Source #
Instances
| (context ~ (), props ~ (), model ~ ()) => ToHtml [View context props model action] Source # | Render a |
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 Rendering never starts the runtime, so a bare |
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