Blog
Chan : New Template and Token model
24 Oct 2009 | 0 Comments | Permalink
After about two months of heavy pondering, design and countless scribbles on A4 paper development on Chan has resumed. The next milestone involves some refactoring of the code but in the past few days the first steps went a lot smoother than expected.
This project will result in a faster rendering engine that is free from the hacks that support panel/fragment region binding that also folds in localisation and an MVC-like token model. If you used Chan's fragments or panels to render into template regions then the only change you will need to make to your code is to replace your template placeholders with square bracketed ones. i.e. replace <!--Region--> with [Region].
The essence of the changes is a reversal in the template-region rendering code. Currently you had to get hold of the Page's Template class instance and set a region string to the value you wanted to appear there. With this release you will simply impliment an ITokenResolver interface method named TokenValue and register yourself with the page itself in a Resolvers collection. The rendering code will then ask your class for its region contents and then - and this is the best bit - parse those recursively, so you can have a class that returns the path to a resourse as "/App_Fragments/some_resourse.ext" and the /App_Fragments token will get parsed out by the built in values garnered from web.config.
The MVC model will provide (very) basic built in formatting functions invoked by prefixxing a token with one or more control chars, so [& TokenName] will cause the value your TokenValue method returns to be HTML encoded, converting & to & on the fly whilst [~ TokenName] will URL encode it and [~& TokenValue] will do both! There will only be a handful of these built in but again you will be able to provide your own formatters if you desire.
Finally how does this all tie into localisation? The first part is URL rewriting, you will be able to silently localise a page by linking to a sub-pathed URL. So to present example.com/Path/Page.aspx in Belgian French you'd link to example.com/fr-BE/Path/Page.aspx and witness the magic unfold. Of course you will still need to provide your own localised strings for each that appears in your page and here you will have multiple options, the simplest will be to provide a string file for your page in a subfolder, something along the lines of example.com/Path/_Page.aspx/fr-BE.locale but you will also be able to use a database too - by implimenting a Locale interface and passing that into the page too. The translated strings will appear in the ASPX (or in the output of any control or other token, remember) as simple tokens, so [MyGreetingString] will be rendered as "Hello" by default, but as "Bonjour" when the Belgian French option is enabled.