Several months ago I blogged about my search for a better view engine for ASP.NET MVC. I mentioned that the current selection of view engines don’t allow developers and designers to work together – none of the engines are restrictive enough in their syntax. Today I would like to present my solution to this problem.
Sharpy is a view engine for ASP.NET MVC based on the popular PHP view engine called Smarty. This view engine is designed with the single goal of allowing developers and designers to work together. This means that a developer should be able to write all the controllers and actions while a designer can create the views.
A simple example
Here is a quick example of Sharpy syntax.
In this example I am using the foreach function (which has an optional else clause which is evaluated if the source is empty or null) as well as simple expressions. There are also 2 variable modifiers being used here – date_format and escape. Variable modifiers can be applied to any expression and can also be combined.
A few details around Sharpy
Sharpy syntax is more restrictive than the regular MVC view engine. You cannot use HTML helpers or extension methods. This means you can use less abstraction and less code in your views – which is a good thing! All forms of abstraction and code make it more difficult for designers and developers to work together – precisely the opposite of what Sharpy was designed for.
This implies that you need to write more of the HTML yourself – the only helpers available are those exposed through functions or modifiers – for example, the Html.Encode method is replaced with the escape modifier. I found the shift to this restrictive type of syntax similar to the move from Webforms to MVC – you need to write more of the HTML by hand. For example, when MVC was introduced one of the first things everybody was looking for was a grid control – and then we very quickly find out we don’t really need it.
The project homepage for Sharpy is on CodePlex. Sharpy is available under the Ms-PL open-source license. To get started download the latest release and take a look at the documentation page for information on using Sharpy in your ASP.NET MVC application.
I have started to write some of the documentation on the project homepage, but this is an ongoing effort. I will however be blogging about most of the features and implementation details in the next few days. The source code also includes 2 example applications to help you get started. In theory you should also be able to look at some of the documentation from Smarty to get a better understanding of what the functions do.
Sharpy was built using TDD which means you can also take a look at the unit tests for examples of how the different functions and modifiers are used.
The way forward
In the next few days I will be blogging about the following details around Sharpy:
- A list of all the functions and modifiers and how they are used
- Expressions in Sharpy
- How to use master pages and partial views
- How to extend Sharpy with your own functions and modifiers
- Performance testing
- How to enable caching
Any and all feedback will be appreciated (positive or negative) – either leave a comment (here or on the project homepage) or contact me directly using the contact form on the right. Happy coding.