Expressions in Sharpy

I recently introduced Sharpy – a view engine designed to allow developers and designers to work together.  Today I’m going to take a look at how expressions work in Sharpy.

How expressions are evaluated

If you’re familiar with Smarty (the PHP view engine Sharpy is based on) you will know that you can pretty much use any PHP syntax and everything will just work.  This is done with black magic.  This is due to the way that Smarty renders views – the entire view (or template) gets converted to actual PHP code which then gets executed.  (This is similar to the way the default ASP.NET view engine works – the entire view gets converted to C# code which is then compiled and executed)

In Sharpy we have to do things a little differently – views are actually interpreted and expressions are evaluated on the fly.  This means there are a few fundamental differences in the way expressions are handled – for example, type safety becomes important .(C# being a strongly typed language and PHP being a dynamic language)  I used the Dynamic LINQ library for evaluating expressions.

What this means when working with expressions

There are a few constraints you need to be aware of when using expressions in Sharpy.  Let’s take a look at an example.

{foreach item="person" source=$model}

It’s important to note that every attribute passed to a function consists of a key-value pair where the value is an expression.  In this case we are dealing with 2 expressions – ‘person’ and $model.  The parser will pick up these expressions, interpret them and pass the evaluated objects to the foreach function.

There is a little bit of fancy footwork going on in the background that makes this possible.  In this case $model is a special variable that is passed from the controller to the view – we need to lookup the value of the variable and pass it to the LINQ expression as a parameter.  (The same would apply if we use any variable from the ViewData dictionary) We also need to rename the $model variable since C# doesn’t allow variable names to start with a dollar ($) sign.  So far so good.

Now let’s say we wanted to do something a little more complicated.  (I would recommend that you try and stay away from really complex expressions like this, simply because it goes against what Sharpy was designed for, but I just want to show that there are no real constraints in these expressions)

{assign var="name" value=$model == null ? "New User" : $model.Name}

If you try this expression in Sharpy you will get a syntax error.  The problem here is that the parser doesn’t know where the expression starts and where it ends.  You will need to use backticks to indicate the start and end of the expression.

{assign var="name" value=`$model == null ? "New User" : $model.Name`}

Basically any expression that contains spaces will need to use backticks.  The only exception here is the if function.  In this case everything between the name of the function and the closing brace will be considered part of the expression.

Making expression easier in Sharpy

There are quite a few modifications I’ve made to expressions in Sharpy to make things a little easier for PHP developers.  For example, the following is valid in Sharpy.

{foreach item='person' source=$model}

In this case the single quotes will be converted to double quotes and C# will interpret this expression as a string value.  Also, you can use any PHP comparison operator.

{if $name eq 'Fred'}

In this case the eq comparison will be converted to a C# == comparison.  You can use any of the following operators – eq, ne, neq, gt, lt, gte, ge, lte and le.  The parser is pretty smart with this kind of thing so string values won’t be incorrectly converted.

Conclusions

There are no real constraints with the expressions you can use with Sharpy.  I didn’t even show examples of things like indexing or casting, but at the end of the day you can pretty much do anything you can do in C#.

Happy coding.