Chris Umbel

Templating with NDjango

It never ceases to amaze me how many great open source tools and libraries have been ported to .Net. NUnit... NHibernate... NAnt... All incredibly widely adopted.

Today, however I'd like to focus one that a coworker brought to my attention which may actually get some use at the office, NDjango.

During my recent Django work I've become quite attached to Django's template language. It took me a while to warm up to it but I'm hooked. When I heard that the templating had been ported to .Net I was naturally quite interested.

Examples

As you'd expect, there really isn't all that much to it. Consider the following code

/* The data we'll pass into the template */
Dictionary<string, object> context = 
	new Dictionary<string, object>();

context.Add("name", "Chris Umbel");
context.Add("profession", "Database Administrator");

/* Create the templating engine */
NDjango.TemplateManagerProvider provider = 
	new NDjango.TemplateManagerProvider();
NDjango.Interfaces.ITemplateManager manager = 
	provider.GetNewManager();

/* Apply the template to the data */
TextReader reader = manager.RenderTemplate(@"C:\template.html", context);

string results = reader.ReadToEnd();
Console.WriteLine(results);

Obviously I need to provide template.txt as specified on line 15.

<table>
    <tr>
        <td>Name:</td>
        <td>{{ name }}</td>
    </tr>
    <tr>
        <td>Profession</td>
        <td>{{ profession }}</td>
    </tr>
</table>

After executing the program the following output is produced:

<table>
    <tr>
        <td>Name:</td>
        <td>Chris Umbel</td>
    </tr>
    <tr>
        <td>Profession</td>
        <td>Database Administrator</td>
    </tr>
</table>

The real power comes into play when you use Django's filters. With some minor modifications to the code:

context.Add("text", "Welcome to the official website and blog of Chris Umbel, database administrator and software developer. ");

and some minor additions to the table in the the template:

<tr>
	<td>Text</td>
	<td>{{ text|truncatewords:10 }}</td>
</tr>    

and I've employed the truncatewords filter resulting in the following output.

<table>
    <tr>
        <td>Name:</td>
        <td>Chris Umbel</td>
    </tr>
    <tr>
        <td>Profession</td>
        <td>Database Administrator</td>
    </tr>
    <tr>
        <td>Text</td>
        <td>Welcome to the official website and blog of Chris Umbel, ...</td>
    </tr>
</table>

Templates can also iterate IEnumerables:

List<DateTime> dateTimes = new List<DateTime>();
dateTimes.Add(new DateTime(2009, 1, 1));
dateTimes.Add(new DateTime(2009, 3, 1));

/* The data we'll pass into the template */
Dictionary<string, object> context = 
	new Dictionary<string, object>();

context.Add("datetimes", dateTimes);

/* Create the templating engine */
NDjango.TemplateManagerProvider provider = 
	new NDjango.TemplateManagerProvider();
NDjango.Interfaces.ITemplateManager manager = 
	provider.GetNewManager();

/* Apply the template to the data */
TextReader reader = 
	manager.RenderTemplate(@"C:\template.html", context);

string results = reader.ReadToEnd();
Console.WriteLine(results);

with a template like:

<table>
    {% for dt in datetimes %}
    <tr>
        <td>{{ dt.Month }}</td>       
    </tr>
    <tr>
        <td>{{ dt.Year }}</td>       
    </tr>    
    {% endfor %}
</table>

produces:

	
<table>

    <tr>
        <td>1</td>
    </tr>
    <tr>
        <td>2009</td>
    </tr>

    <tr>
        <td>3</td>
    </tr>
    <tr>
        <td>2009</td>
    </tr>

</table>

Conclusion

I've just covered a basic example here. The possibilities with NDjango are pretty much limitless, however.

For some inspiration it's worth investigating some of the integrations already done such as Bistro and ASP.Net MVC.

Sun Sep 06 2009 17:09:00 GMT+0000 (UTC)

Follow Chris
RSS Feed
Twitter
Facebook
CodePlex
github
LinkedIn
Google