Skip to content

Instantly share code, notes, and snippets.

@thefringeninja
Created February 3, 2015 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefringeninja/29e4904ded45f38dbbed to your computer and use it in GitHub Desktop.
Save thefringeninja/29e4904ded45f38dbbed to your computer and use it in GitHub Desktop.
FSharp and DotLiquid
open System.Linq
let isSimpleType (typ:Type) =
typ.IsPrimitive || typ.IsEnum || primitiveTypes.Contains typ
open System.Collections
open System.Reflection
let rec private proxy(target : obj) =
let maybeTarget = Some target
let result = match maybeTarget with
| Some t ->
let flags = BindingFlags.Instance ||| BindingFlags.NonPublic ||| BindingFlags.Public
let properties = target.GetType().GetProperties(flags)
properties
|> Seq.map(fun property ->
match isSimpleType property.PropertyType with
| true -> property.Name, property.GetValue(target)
| false -> property.Name, proxy(property.GetValue(target)) :> obj)
|> dict
| None -> Seq.empty |> dict
Hash.FromDictionary(result)
let render(view : View) =
let template = getTemplate(view.route)
match view.model with
| null -> template.Render()
| _ ->
let liquidModel = proxy view
template.Render liquidModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment