General Syntax

Basic markup

A view file has basic html markup that can appear pretty much as you'd expect.

<div>
  <p>Hello <span>world</span>!</p>
</div>

Including code

CSharp code may be used to produce output. Any helper methods, object properties, or expression that evaluates to a non-void value can be used.

<p>${"Hello world"}</p>
<p>${Guid.NewGuid().ToString("n")}</p>

Code will also appear in special attributes or elements like "if", "else", and "var". These types of areas will never require ${} or <%= %> notation.

<var names="new [] {'alpha', 'beta', 'gamma'}"/>
<for each="var name in names">
  <test if="name == 'beta'">
    <p>beta is my favorite.</p>
    <else/>
    <p>${name} is okay too I suppose. 
  </test>
</for>

However if you use code to produce a normal attribute's value, that is not a special attribute does require the code-output notation.

<viewdata currentProduct="Product"/>
<p>${currentProduct.Name} <a href="/Product/Edit/${currentProduct.Id}">Edit</a></p>

Special characters in code

The entire functionality of the csharp language is available. As always, please use responsibly. :) Be sure to put business logic in your controller's actions and write unit test for it.

There are a small number of characters that are parsed in a special way to avoid simple mechanical problems you can encounter in a standard xml editor.

The sequences [[ and ]] are parsed as if they are < and >. This can be convenient when you're using generic classes or methods from a spark file, to avoid the editor's tendency to add a closing elements for you. The [[ and ]] will not be changed if they occur inside a string literal.

String literals may be expressed in 'single quotes' in addition to "double quotes". This is again for convenience when using expressions inside attributes values that are contained in double quotes. If you do need to use a single char constant in an expression, using an explicit type-cast (char)'x' will avoid having the string changed to "x".

<viewdata products="IList[[Products]]"/>
<var styles="new[] {'even', 'odd'}"/>

Spark element prefix with config

By default, Spark tags are unqualified. All of the examples you'll see are written that way. That can cause a problem if Spark is being used to create xml rather than xhtml. One of the ways to disambiguate spark xml from output xml is to set a prefix for your application.

<spark>
  <pages prefix="s"/>
</spark>

Or

var settings = new SparkSettings()
    .SetPrefix("s");

At which point all of the native spark elements and attributes must include the s: prefix to be recognized. None of the built-in prefixes macro:, content:, use:, render:, and section: are affected by the use of the prefix setting.

<div>
  <s:viewdata user="UserInfo"/>
  <p s:if="user != null">${user.Name}</p>
  <s:LoginForm/>
</div>

Spark element prefix with xmlns namespaces

Another way to disambiguate the markup is to declare xmlns attributes, which will result in well-formed and valid xml. All of the Spark namespace Uris begin with http://sparkviewengine.com/. If a template file contains a Spark xmlns attribute then the other built-in prefixes (macro:, etc.) will only function if their namespace Uri's are also declared.

Default prefixNamespace Uri
none - spark tags and partial fileshttp://sparkviewengine/
content:http://sparkviewengine/content/
use:http://sparkviewengine/use/
macro:http://sparkviewengine/macro/
section:http://sparkviewengine/section/
render:http://sparkviewengine/render/

You can use the defaults or specify any prefix you choose. Once an xmlns attribute is defined only the value on the namespace Uri is significant.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:s="http://sparkviewengine.com/"
      xmlns:fn="http://sparkviewengine.com/macro/">
<body>
  <fn:ShowNames favorite="string">
    <s:var names="new [] {'alpha', 'beta', 'gamma'}"/>
    <s:for each="var name in names">
      <s:test if="name == favorite">
        <p>${favorite} is my favorite.</p>
        <s:else/>
        <p>${name} is okay too I suppose.
      </s:test>
    </s:for>
  </fn:ShowNames>
  ${ShowNames("beta")}
  ${ShowNames("gamma")}
</body>
</html>