views:

39

answers:

2

I am thinking about a really simple html templating system to implement in asp.net.

Basically given a .html file with a couple of placeholders like [menu] and [content] it would grab the appropriate data and merge it with the html and the display the page. I'd like to be able to use asp.net stuff like controls and postback but this isnt essential.

How difficult would this be to implement it and how would you go about doing it? Also is a solution like this practical for a pretty low traffic cms website?

A: 

You could write an HTTP Module to redirect all incoming requests to a single ASPX page.

This page could examine the original URL to work out what data to retrieve from the database and display it to the user.

As the page is a standard ASPX page, you'll still be able to take advantage of post-backs, server controls etc.

If you need pages that have different layouts and functionality, you could write a series of ASPX pages (treating each one as a different 'template', effectively). Then, in your HTTP Module, you could look the page up in the database to work out which 'template' ASPX page you should redirect to.

There's more information about writing an HTTP Module to redirect URLs here. Make sure you read down to the part which tells you how to override the default form action to make sure your post-backs still work!

Hope this helps - good luck!

Chris Roberts
+2  A: 

Writing your own template system is basically reinventing the wheel in ASP.NET. I wouldn't recommend it unless you plan on learning more about wheels (in which case I'd say go for it).

A better alternative would be to use the one that Microsoft provides as part of the framework, with Master Pages. Microsoft has figured out the hard details of implementing a template system and even lets you nest templates within templates. You can dynamically swap templates in & out if you want a different look.

For a CMS, I would say you're better off using master pages than creating your own.

Dan Herbert
yeah I normally would, was just thinking in terms of providing a format more familiar to designers than masterpages.
Luke Lowrey