Sharpy functions and modifiers

In my last post I introduced Sharpy – a view engine designed to allow developers and designers to work together.  Today I’m going to list all the different functions and modifiers available and give a short description of how each works.

Update: I have updated the documentation on the project homepage with all the information here and also added examples for all variable modifiers.

As I mentioned in my last post I designed Sharpy with extensibility in mind.  This means I had to create interfaces to allow other developers to write their own functions and modifiers.  To accommodate this I have identified 3 different types of functions – block functions, inline functions and expression functions.

Block Functions

Block functions, such as foreach, have both a start and end tag with content between the tags.

Capture – all the content between the tags is evaluated and stored into the specified variable

{capture name='captured'}
    <p>This is captured output</p>
{/capture}

Foreach – iterates through the specified list of items.  A foreachelse tag can also be used and will be evaluated if the source list is null or empty.  Note – the list of items must be of type IListUpdate: In the latest release this has been changed to IEnumerable.

{foreach from=$numbers item='number'}
    <li>{$number}</li>
{/foreach}

Literal – allows a block of data to be taken literally.  Typically used around Javascript or stylesheet blocks where the markup would be incorrectly interpreted.

{literal}
function serverName(name, ip){
   alert("Current server\n" + name + "\n" + ip);
}
{/literal}

Strip – removes all unnecessary whitespace and carriage returns

{* will be put into one line upon output *}
{strip}
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
{/strip}

Expression functions

Expression functions are similar to block functions, except that the entire opening tag is treated as an expression.

*If **– evaluates the expression as a boolean value and executes the appropriate content.  Can also be used with a *else or elseif tag.

{if $name eq 'Bob'}
    <p>Hello, 'Bob'</p>
{elseif $name eq 'Steve'}
    <p>Hello, 'Steve'</p>
{else}
    <p>Hello, '{$name}'<p>
{/if}

Inline functions

Inline functions contain a single tag – there is no closing tag or content.

Assign – assigns a value to a variable.

{assign var='name' value='Bob'}

Cycle – used to alternate a set of values.  Typically used to alternate between two or more colours in a table.

{foreach from=$Model item='number'}
    <li style='background-color:{cycle values='#eeeeee,#d0d0d0'}'>{$number}</li>
{/foreach}

Include – used to include a partial view.

{include file='~/Views/Home/Partial.sharpy'}

LDelim, RDelim – used to escape the syntax delimiters, { and }.

<p>The delimiters in Sharpy are {ldelim} and {rdelim}</p>

Variable Modifiers

Variable modifiers can be applied to any expression.  To apply a modifier, specify the expression followed by a (pipe) and the modifier name.  Modifiers can accept additional parameters.  These parameters follow the modifier name and are separated by a : (colon).  Modifiers can also be combined, for example {$title upper escape}.
  • Capitalize – used to capitalize the first letter of all words
  • Cat – used to concatenate strings
  • Count characters – used to count the number of characters
  • Count paragraphs – used to count the number of paragraphs
  • Count sentences – used to count the number of sentences
  • Count words – used to count the number of words
  • Date format – formats a date variable according to the specified format
  • Default – used to specify a default value for a variable in the case the variable is null or empty
  • Escape – similar to the Html.Encode method
  • Indent – used to indent a string on each line
  • Lower – used to convert a variable to lowercase
  • Nl2br – converts all line breaks to
    tags
  • Regex replace – performs a regular expression search and replace
  • Replace – performs a simple string search and replace
  • Spacify – inserts a space between every character
  • String format – similar to the String.Format method
  • Strip – replaces all repeated spaces, newlines and tabs with a single space (or the supplied string)
  • Strip tags – strips out markup tags, basically anything between < and >
  • Truncate – used to truncate a variable to a character length
  • Upper – used to convert a variable to uppercase
  • Word wrap – used to wrap to a column width

Most of the variable modifiers have optional parameters to modify their behaviour.  For example, the word wrap function wraps to a default column width of 80 and uses a newline to wrap words.  If we wanted to wrap on a column length of 30 characters and use a

<br />

to wrap words we would use

{$name|wordwrap|30|’<br />’}
I am still in the process of updating the documentation on all the different functions and modifiers.  If you’re looking for more detailed information and you can’t find it on the

project homepage your best bet would be to look at the Smarty documentation or have a look at the unit tests. 

Happy coding.