Getting started
Usage
FSharp.ViewEngine integrates with Giraffe by rendering elements to an HTML string and returning it via Giraffe's htmlString handler.
Minimal Example
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Giraffe
open FSharp.ViewEngine
open type Html
let indexView =
html {
_lang "en"
head {
title "My App"
meta { _charset "utf-8" }
}
body {
h1 { "Hello from FSharp.ViewEngine!" }
p { "Served by Giraffe." }
}
}
let indexHandler : HttpHandler =
fun next ctx ->
let html = Render.toHtmlDocString indexView
htmlString html next ctx
let webApp =
choose [
GET >=> route "/" >=> indexHandler
]
[<EntryPoint>]
let main args =
let builder = WebApplication.CreateBuilder(args)
builder.Services.AddGiraffe() |> ignore
let app = builder.Build()
app.UseGiraffe(webApp)
app.Run()
0
How It Works
- Build your HTML using FSharp.ViewEngine elements
- Call
Render.toHtmlDocStringto get a<!DOCTYPE html>string (orRender.toStringfor a fragment without the doctype) - Return it with Giraffe's
htmlStringhandler
That's it — no special adapter or middleware needed.