Today we launched WebMatrix Beta 2 with a bunch of new features. Here are my favorites:
- Themes – quickly switch between different themes on your website
- New HTML5 Templates in the box – rounded corners, transitions, sexy HTML5 templates. yum.
- NuPack Package Management – get the bits you need, when you need em
- New @Helper Syntax – adding helpers is now even easier
The new @Helper syntax makes it super easy to create your own helpers for WebMatrix. Why would you want to do that? Well, there’s a couple of reasons. Firstly, as a timesaver. If you find yourself writing the same code or HTML time after time in your websites, it might be time to package up your code in a reusable Helper that you can utilize throughout your websites instead of writing the same thing over and over again and wasting time. Secondly, if you are trying to foster a developer community that can use your service or website, then creating WebMatrix Helpers to make their life easier is a smart thing to do – because it removes friction for people to use your service.
Take the new Facebook Social Plugin Helpers in WebMatrix Beta 2 as an example. They make use of the new @helper syntax to make it easier for developers to add social widgets to web pages. The way you get these helpers is also worth mentioning, because it highlights a use of the NuPack technology that we announced today also. Facebook’s Social Plugins Helper includes shortcuts to the following social widgets:
- Login Button
- Like Button
As well as the ability to easily add metadata which shows up in rich ways across Facebook.
Getting your Helpers using NuPack
NuPack is an open source project driven by Microsoft that provides package management for developers in WebMatrix or Visual Studio 2010. Within WebMatrix there is a package management page that you can visit where you can manage which packages are installed in your website projects. With NuPack, it’s super easy to get Helpers and other things you’ll need to develop your website.
Note - the current structure of our website if we create a new website from template. Inside our app_data folder we have a bunch of folders including packages. This is where the assets for our packages live. We can see that we already have microsoft-web-helpers.1.0 installed by default and this package provides Helpers like Twitter, ReCapture and more. There is also a packages.config file which contains information which points WebMatrix to the packages installed in App_Data.
To manage the packages installed in our website, we need to get to the admin page by simply navigating to http://localhost:xxx/_admin where xxx is your port number. (Note – by default this is only enabled when running websites locally, as a security precaution). If it’s the first time you’ve visited the page, you’ll be asked to create a password, remember this for future visits to the package manager page.
Next, you’ll be presented with a list of packages that you can install to use within your website. Clicking on the Install button next to the Facebook.Helper 1.0 will install the files and pre-requisites for the helper to work correctly. In the case of the Facebook Helper there are no dependencies however.
Once installed, the package “explodes” (in a useful, not violent way) in your website, installing various assets. If we now look at the structure of the website in WebMatrix we see the Facebook Helper package has been installed successfully:
The most important asset is the Helper itself – FacebookSocialPlugin.cshtml and you can see that it was installed under the App_code folder in the root of website. In this particular helper, we’ve also included some docs which explain how to get started as well as a full reference for all the methods in the helper.
To get to the docs just navigate to http://localhost:xxx/facebook/docs/starthere.htm
Note – There is already a Facebook Helper included in the Microsoft-web-helpers library which provides a shortcut to adding the Like Button to your website. The FacebookSocialPlugin helpers has the like button as well as all the other Facebook Social Plugins and other good stuff like Open Graph etc. In the next release we’ll merge the two Helper libraries so they are consolidated.
Let’s explore the Facebook Social Plugin Helper a little more to understand how to use the @helper syntax. The naming of the FacebookSocialPlugins.cshtml file is a good place to start. This name will be taken as the class name at runtime, so the developer will use FacebookSocialPlugins.Foo() when using the helpers within the file. Exploring the file, we see that all the methods use the @helper syntax, which mixes markup and code so that it’s possible to build up quite complex HTML and then inject parameters from the method or from calculated values in the helper code itself.
Notice how we inject he AppId from the FacebookSocialPlugins class? This notion of mixing code and markup within a class method is very powerful and useful in lots of ways. There’s a couple additional steps that you must do in order to have the Social Plugins working including calling the following method in _AppStart.cshtml.
Secondly you must register the Facebook namespace in _Layout.cshtml which tells the page about the Facebook markup:
Normally this would be a copy and paste from the depths of the Facebook developer portal, now it’s simply a case of using the Helper and you’re done.
This then clears the path for us to easily add Facebook’s Social Plugins to a WebMatrix website:
It’s important to note that some Social Plugins require your site to be published to an external host that Facebook can communicate back and forth to.
I recommend checking out the documentation which comes as part of the Helper NuPack for more information on getting started, simply press the “run” button in WebMatrix with startHere.html opened to browse the docs.
Other good reading on WebMatrix Beta 2
- Scott Guthrie’s blog post
- Channel 9 Interview with Scott Hunter and Erik Porter
- Top resources for getting started with WebMatrix