The easy way is to use toResponseBS
:
import Data.ByteString.Char8 as C
import Data.ByteString.Lazy.Char8 as L
svgToResponse svg = toResponseBS (C.pack "image/svg+xml") (L.pack $ "<svg>" ++ svg ++ "</svg>")
Another option is to make a ToMessage
instance:
newtype SVG = SVG String
instance ToMessage SVG where
toContentType _ = C.pack "image/svg+xml"
toMessage (SVG svg) = L.pack $ "<svg>" ++ svg ++ "</svg>"
Now you can do toResponse (SVG svg)
or even ok $ SVG svg
but the latter will not compose well with non-SVG handlers via msum
...
edit: The above is relevant for serving an SVG file from a handler but you actually asked for embedding SVG in HTML which I first missed.
The answer depends on how you're generating your HTML.
If you're using blaze-html
you want to use the preEscapedToHtml
function:
preEscapedToHtml $ "<svg>" ++ svg ++ "</svg>"
If you're using HSP you want to use the cdata
or pcdata
function (I can never remember which, so try both):
<svg><% cdata svg %></svg>
Hope that helps!