All variables start with a $ (dollar) sign and the same naming rules apply as in C#. Sharpy uses Dynamic LINQ to evaluate variables and expressions.

Constraints

The following example is a common usage of a function in Sharpy:

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

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 example we are dealing with 2 expressions - "person" and $people. The parser will pick up these expressions, interpret them and pass the evaluated objects to the foreach function.

You can reference any variable out of the ViewData by name - in this case we could have a variable stored in the ViewData with the key 'people'. You can also reference the model for the view by using $model.

There are no real constraints in the expressions - any valid C# expression can be used. However, you may need to indicate the start and end of your expressions. For example, the following valid C# expression will cause a syntax error in Sharpy.

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

The problem here is that the parser doesn't know where the expression starts and 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`}

Using PHP Expressions

Since Sharpy is based upon a PHP view engine certain modifications have been made to expressions in an effort to make things a little easier for PHP developers. Firstly, single quotes can be used to indicate string values.

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

The parser will convert these to double quotes before evaluating the expression. Certain PHP comparison operators can also be used.

{if $name eq 'Fred'}

In this case the eq operator will be converted to a C# == operator. The following PHP operators can be used in Sharpy - eq, ne, neq, gt, lt, gte, ge, lte, le. The parser can identify string values and won't incorrectly convert them.

Last edited Feb 17, 2010 at 1:37 PM by JacoPretorius, version 5

Comments

No comments yet.