Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Source Code for Table EDSL In F#

with one comment

Here’s the entire source code for the post An EDSL for Emitting HTML Tables in Web Parts using F#.

#light
namespace FSWebPart

open System.Web
open System.Web.UI
open System.Web.UI.WebControls
open System.Web.UI.WebControls.WebParts

type CellContent =
    | Static of string
    | Control of WebControl
    | InnerTable of TableContent list
and TableContent =
    | Row
    | Cell
    | Content of CellContent

type TableEDSL() =

    let mutable haveCell = false
    let mutable haveRow = false

    member x.CheckCellClosure(writer:HtmlTextWriter) =
        if haveCell then
            writer.Write("</td>")
            haveCell <- false

    member x.CheckRowClosure(writer:HtmlTextWriter) =
        if haveRow then
            writer.Write("</tr>")
            haveRow <- false

    member x.WriteTable (theTable, writer:HtmlTextWriter) =
        writer.Write("<table>")
        List.iter (fun ele -> x.Table (ele, writer)) theTable
        x.CheckCellClosure(writer)
        x.CheckRowClosure(writer)
        writer.Write("</table>")
           
    member x.Table (theTable, writer:HtmlTextWriter) =
        match theTable with
            | Row ->
                haveRow <- true
                x.CheckCellClosure(writer)
                writer.Write("<tr>")
            | Cell ->
                x.CheckCellClosure(writer)
                haveCell <- true
                writer.Write("<td>")
            | Content s ->
                match s with
                    | Static str -> writer.Write(str)
                    | Control ctl -> ctl.RenderControl(writer)
                    | InnerTable tbl -> x.WriteTable(tbl, writer)               

type public HelloWorldWP() =
    inherit WebPart()

    let labelRow1 = new Label()
    let inputRow1 = new TextBox()
    let inputRow2 = new TextBox()
   
    override x.CreateChildControls() =
        labelRow1.Text <- "Row 1 Label"
        x.Controls.Add(labelRow1)
        x.Controls.Add(inputRow1)
        x.Controls.Add(inputRow2)
               
    override x.Render (writer:HtmlTextWriter) =
        let myTable = [
                        Row
                        Cell
                        Content (Control labelRow1)
                        Cell
                        Content (Control inputRow1)
                        Row
                        Cell
                        Content (Static "Row 2 Static")
                        Cell
                        Content (Control inputRow2)
                        ]
        let tedsl = new TableEDSL()
        tedsl.WriteTable(myTable, writer)               

Written by Nick Grattan

May 12, 2010 at 6:53 pm

One Response

Subscribe to comments with RSS.

  1. […] Click Here for a page showing all the source code. Possibly related posts: (automatically generated)Creating Web Parts with F# […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: