Let me show the simplest possible self-contained example involving WebSharper sitelets that are coming with the 2.1 release. You define a website with two pages (and two actions):
Let us quickly mash up a template for the website using F#-embedded HTML combinators. A template is just a function taking the body and decorating it:
Two things to notice:
- F# lets you define your own syntax, and the example makes liberal use of that (=>).
- Instead of generating URLs by hand, you ask the context to create a link to the action. This ensures safety: renaming Home action makes the project stop compiling, and moving it to a different URL keeps the links correct.
Now, you define the sitelets:
HomePage and AboutUsPage are single-page sitelets, with a single URL and a single Action. They are combined into a website by the Sum operator.
Now, a little bit of administrative boilerplate:
And you are done! Let’s browse:
So far so good. The pages have the expected URLs and the menu links work.
So there is a button that changes its title when clicked. AND there is a control. Now, this while the Button lives on the client-side completely (constructs DOM nodes, in fact), the Control executes a quantum leap: the constructor executes on the server, and the body on the client. But that means you can use the Control to glue the server and the client together. Let us update the AboutUs page:
Below is the complete listing. As soon as the 2.1 release becomes public, you will able to enjoy running it yourself. Stay tuned!