crli
Nov 16, 2010
  5320
(3 votes)

Spark View Engine on WebForms

Spark view engine is a neat little web utility that allows you to write code like this:

<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
  <li each="var p in products">${p.Name}</li>
</ul>
<else>
  <p>No products available</p>
</else>

If your eye skipped over it because it looks just like html I wouldn’t blame you. But look again. Loops and conditions are nicely embedded in html. Spark really does let the html dominate (which can be a good thing). These are but a few of all the nice features. If you have them, spend a couple of minutes browsing the spark documentation to learn what’s more.

To learn a bit about the inner workings of this wonderful tool I set out to add WebForms support. The spark codebase has a very testable design and can be coerced to help out in a WebForms control without modification. The end result look like this:

<spark:View runat="server">
  <ul if="Container.GetProducts().Any()">
    <li each="var p in Container.GetProducts()">${p.Name}</li>
  </ul>
  <else>
    <p>No products available</p>
  </else>
</spark:View>

Download and usage

To use it download

and unzip into your bin folder.

Register the control in web.config:

<pages>
  <controls>
    <add tagPrefix="spark" namespace="Spark.Web.Forms" assembly="Spark.Web.Forms"/>

Add something useful to iterate over to your page or user control:

public string[] Values;
protected void Page_Load(object sender, EventArgs e)
{
  Values = new string[] { "Simple", "As", "Pancake" };
}

And add the spark control:

<spark:View runat="server">
  <ul>
    <li each="var c in Page.Values">${c}</li>
  </ul>
</spark:View>

Future improvement ideas

Some features are definitely missing (and they might require modification of the spark code itself):

  • Spool content from a spark view controls placed later in the control tree
  • Cache the compiled views
  • Support the spark cache element

The code is over here.

Nov 16, 2010

Comments

Nov 16, 2010 10:17 PM

Its good to see the new rendering engines coming out. This looks promising but so does Razor - who will win :)!

Magnus Rahl
Magnus Rahl Nov 17, 2010 08:10 AM

Is it possible to use Razor with WebForms?

Stefan Forsberg
Stefan Forsberg Nov 17, 2010 08:54 AM

Awesomesauce Cristian!

Nov 17, 2010 10:31 AM

@Magnus On Scot Gu's blog he says to "watch this space" when talking about Razor with classic web forms. Not sure when that will be though!

Magnus Rahl
Magnus Rahl Nov 17, 2010 01:20 PM

Thanks David! Maybe I'll go with Spark for now then, it is indeed nice to be able to do loops etc, and to do it without butt ugly syntax.

Stefan Forsberg
Stefan Forsberg Nov 17, 2010 01:50 PM

@Magnus, is there a particular feature from Razor that you're missing in sparks?

Magnus Rahl
Magnus Rahl Nov 17, 2010 07:03 PM

@Stefan, no just curious :)

Deane Barker
Deane Barker Nov 18, 2010 02:56 AM

For one client, we actually allowed them to have Spark code in an EPIServer XHTML property. When the page rendered, a Page Adapter executed the Spark markup, so they could alter the HTML output down to the single word or sentence based on external criteria.

Joel Abrahamsson
Joel Abrahamsson Nov 24, 2010 06:39 PM

Awesome work! I would really love it if you would continue improving this as I think this could be extremely useful, especially combined with MVP.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024