Jab2 - Theming with DarkView

Tuesday, 11 January 2011

Inspired by tumblr's theming support, Jab gets it's own flavour of something similar.

DarkView is a simple template engine that I wrote sometime ago for a different project, but I've never publicly released it. DarkView is designed as a way for end-users to theme content.

Unlike typical ASP.NET view engines such as webforms, Razor View, Spark View etc..., DarkView provides enough to do fairly complex theming but not so much to give end-users control over your server.

DarkView Mini Samples

Writing DarkView templates is pretty easy. Variables are delimited with curly braces and automatically html encoded. eg:

<title>{title}</title>

Conditional code is through an if block:

{if weekend}
    <p>Welcome to the weekend</p>
{else}
    <p>It's a weekday - go to work</p>
{/if}

Loops with a foreach block:

{foreach article}
    <h1>{title}</h1>
   {body}
   <p>by {author}</p>
{/foreach}

The above examples use simple variables, but there's also a fairly complete expression engine and methods can also be called:

<p>Total with GST: {(amount*(1+gst)).ToString("#.##")}<p>

Logical operators too:

{if !published && saved}
    <p>This is a draft</p>
{/if}

Variable Scope

One of the interesting features of DarkView is the way it resolves variable names through a stack of objects. Whenever a loop is executed, the current object being iterated is pushed onto this stack at the start of the loop iteration and removed at the end. In resolving variables the stack is walked looking for any object with a matching property.

If you take a closer look at the foreach example above, the title, author and body variables are all resolved using the current article. Although dot member notation is supported it's generally not needed and non-programmer end-users seem to understand this syntax more readily.

Using DarkView

Using DarkView programatically is very simple.

  1. Define one or more "model" objects that expose public methods and properties that you want the template to have access to. You may be able to use existing business/ORM objects so long as everything is safe for end-user access.

  2. Use the DarkView Parser to parse the DarkView script into a template:

    var Template=DarkView.Parser.Parse("theme.darkview");
    
  3. Execute the template: (where model is the object from step 1)

    var result = Template.Render(model);
    

All going well, the template will be executed and result will contain the final text.

One final note, by default DarkView will Html.Encode all string results. If you have a variable returns pre-rendered HTML (say the result of MarkdownDeep processor), use the HtmlString type to indicate this:

public DarkView.HtmlString body
{
    get
    {
        var md = new MarkdownDeep.Markdown();
        md.SafeMode = false;
        md.ExtraMode = true;
        md.AutoHeadingIDs = true;
        md.MarkdownInHtml = true;
        md.NewWindowForExternalLinks = true;
        return new DarkView.HtmlString(md.Transform(this.content));
    }
}

Integration into Jab

So that's DarkView in a nutshell. There's nothing terribly exciting to say about it's integration into Jab except that it's done. It looks for an uploaded file named theme.darkview that the user can edit to template his site. Failing that it will use a pre-canned default theme which at the moment is very minimal but works:

JabThemeAndDomain.png

Note the custom domain and custom theming.

Next I'll be looking at adding an RSS feed.

« Jab2 - Domain to Site Mapping Jab2 - RSS Feed »

Leave a comment

Name (required)
Email (required, not shown, for gravatar)
Website (optional)
Your Message
Leave these blank: