Click here to Skip to main content
15,893,381 members
Articles / Web Development / HTML

SharpTemplate - A strongly typed HTML templating solution for Script#

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
15 Jan 2010CPOL5 min read 23.6K   140   16  
SharpTemplate enhances Script# by providing statically compiled, strong typed, super efficient HTML templating. This article is a tutorial for using SharpTemplate.
//-----------------------------------------------------------------------------
// <auto-generated>
// TutorialTemplate.cs
//
// Do not edit directly. This file has been generated by SharpTemplate 1.0.0.0
// </auto-generated>
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.DHTML;
using System.XML;

using SysScript = System.Script;

namespace DemoScript
{
	public static class TutorialTemplate
	{
		private static int _Cube(int i)
		{
			return i * i * i;
		}
		
		public static string Render(ArrayList b)
		{
			bool parentBuffer = (b != null);
			b = b ?? new ArrayList();
			b.Add(Script.Literal(@"'<div>'"));
			RenderPlainHTML(b);
			b.Add(Script.Literal(@"'</div><div>'"));
			IntersperseCode(b, 3);
			b.Add(Script.Literal(@"'</div><div>'"));
			EmbedExpression(b, 3);
			b.Add(Script.Literal(@"'</div><div>'"));
			EmbedExpression2(b, 3);
			b.Add(Script.Literal(@"'</div>'"));
			return parentBuffer ? "" : b.Join("");
		}
		
		public static string RenderPlainHTML(ArrayList b)
		{
			bool parentBuffer = (b != null);
			b = b ?? new ArrayList();
			b.Add(Script.Literal(@"'<p>SharpTemplate has the below advantages...</p><ul><li>Compile time type checking</li><li>Leverages .Net tools and Visual Studio IDE</li><li>Super efficient compared to client side template engines</li></ul>'"));
			return parentBuffer ? "" : b.Join("");
		}
		
		public static string IntersperseCode(ArrayList b, int count)
		{
			bool parentBuffer = (b != null);
			b = b ?? new ArrayList();
			b.Add(Script.Literal(@"'<ui>'"));
			for(int i = 0; i < count; ++i)
			{
				b.Add(Script.Literal(@"'<li>Script# with SharpTemplate is a great combo!</li>'"));
			}
			b.Add(Script.Literal(@"'</ui>'"));
			return parentBuffer ? "" : b.Join("");
		}
		
		public static string EmbedExpression(ArrayList b, int count)
		{
			bool parentBuffer = (b != null);
			b = b ?? new ArrayList();
			b.Add(Script.Literal(@"'<ui>'"));
			for(int i = 1; i <= count; ++i)
			{
				b.AddRange(new object[] {
					Script.Literal(@"'<li>'"),
					i,
					Script.Literal(@"' squared is '"),
					i * i,
					Script.Literal(@"'</li>'")
				});
			}
			b.Add(Script.Literal(@"'</ui>'"));
			return parentBuffer ? "" : b.Join("");
		}
		
		public static string EmbedExpression2(ArrayList b, int count)
		{
			bool parentBuffer = (b != null);
			b = b ?? new ArrayList();
			b.Add(Script.Literal(@"'<ui>'"));
			for(int i = 1; i <= count; ++i)
			{
				b.AddRange(new object[] {
					Script.Literal(@"'<li>'"),
					i,
					Script.Literal(@"' squared is '"),
					i * i,
					Script.Literal(@"', cube is '"),
					_Cube(i),
					Script.Literal(@"'</li>'")
				});
			}
			b.Add(Script.Literal(@"'</ui>'"));
			return parentBuffer ? "" : b.Join("");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect www.globalscholar.com
United States United States
I hold a bachelors degree in Computer Science & Engineering and am currently a Software Architect at GlobalScholar working on cutting edge technological solutions for education. Prior to this, I was at Microsoft working on Microsoft Outlook and Microsoft Office Live. My areas of technical specialization include Web/Ajax, Microsoft .Net, compilers and microprocessors. Other than work, I spend quite some time reading latest happenings in science/technology, hacking out some pet projects, playing musical instruments and being in yoga.

Comments and Discussions