The conception, birth, and first steps of an application named Charlie

Subscribe: Atom or RSS

Recursion is not a Dirty Word

by Alister Jones (SomeNewKid)

Here is an example of the “raw” templating system to be implemented by Charlie.

<html>
    <head>
    </head>
    <body>
        <insert:Snippet name="Header" />
        <h1><this:Title /></h1>
        <this:Content />
    </body>
</html>

If the Snippet to be inserted was simple text, then this templating system could be implemented with a series of find-and-replace operations. However, the inserted Snippet itself may include another <insert> element:

<div id="header">
    <h1>Company Name</h1>
    <insert:Snippet name="SearchBar" />
</div>

More troubling, that child Snippet may include an <insert> element that grabs not another Snippet, but a Text Resource:

<div id="searchbar">
    <insert:Text name="Search" />
    <input name="SearchTerm" />
</div>

This series of inserts within inserts within inserts is the age-old programming challenge of recursion. Fortunately, ASP.NET’s existing web server control system makes short work of recursion. ASP.NET allows us to place a single web server control onto a page, and that control may contain child controls. And those child controls may contain more controls. And so on.

Even better, ASP.NET provides a ParseControl method that allows us to pass in a string (exactly like the snippets shown above) and receive a Control in return. We can then plonk that control onto our web page, at which point ASP.NET’s page lifecycle will take over. In other words, this single method allows us to go from a simple string to a fully-functional, recursion-enabled web server control that can participate in all of ASP.NET’s goodies such as state management, event handling, and output rendering.

So while I have chosen to ignore ASP.NET’s built-in master pages, themes, skins, and so on, I will definitely make use of its web server control system. Anyone who has ever authored a custom web server control knows that this system is relatively simple, yet infinitely flexible. And you all know my goals for Charlie: keep it simple and keep it flexible.

by Alister Jones | Next up: Implementing Raw Templating - Part 1

0 comments

----