Learn More





DNN Community Blog

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.

The Repository 4.0 Template engine overview

As part of the total re-write of the Repository module, I am rewriting the template engine to be more powerful, more flexible and more useful than ever. A lot of the enhancement requests could be addressed through a more flexible and extensible template engine, so I have taken a more traditional approach to the engine as opposed to the current 'token replace' technology.

I have also refactored the template engine into a reusable public class in case you would like to take advantage of templating within your own DotNetNuke modules.

Before I get into the details, here's a quick snippet to whet your appetite and show you how you would use the template engine in your code. Hopefully this little snippet will let you see how much the engine has changed from it's previous usage. This is the simplest example, there are many ways to 'load' a template and many ways to set the values of the tokens. I will cover those other options later in the article.

Dim template as New Repository.Template()
template.LoadString = "Hello [FIRSTNAME] [LASTNAME]"
template.SetToken("FIRSTNAME", "Steve")
template.SetToken("LASTNAME", "Fabian")
Dim results as string = template.ToString()
' results will be "Hello Steve Fabian"

So, let's talk about the 'template' object.

A Repository template has a 'source' property which represents the template 'instructions'. Much like the the current template function being used in the 3.X version and the TokenReplace functionality elsewhere in the core, it includes static text and tokens delimited by square brackets. In the previous example, "Hello [FIRSTNAME] [LASTNAME]" is the template source, and FIRSTNAME and LASTNAME are the tokens.

In order to use a template you need to follow these steps...

1. create a new template object
2. set the template source
3. set the token values
4. tell the template to process the source and render its' output

1. Create a new template object

There are a number of ways to create a new template object, calling the plain initializer then setting the properties individually, or using one of the other initializer functions to create and initialize some properties in one call.

' basic initializer function
Dim template As New Repository.Template()
' initialize and set the template source
Dim template As New Repository.Template(templateString)
' initialize and set the current module id and local resource file
Dim template As New Repository.Template(moduleid, localresourcefile)
' initialize and set the current portal id, moduleid and resource file
Dim template As New Repository.Template(portalid, moduleid, localresourcefile)

2. Set the template source

Once you have a template object, the next step is to set the source. As with the initialization step, there are a couple of ways to set the source. Setting the source using one of these 2 methods will trigger the template Parse() function to determine what static content and what tokens are included in the template.

' pass a string as the source
' load the contents of a text file into the template source

3. Set the Token values.

Here's the meat of the template engine. Now that a template has been loaded and parsed, you need to 'marry' up the data values to the tokens. You can set each token individually..

template.SetToken(tokenName, tokenValue)

The direct method works fine for simple templates with a small number of tokens, however, in order to make it easy to load up a large number of tokens, you can pass any 'object' and the template SetToken method will iterate through all available tokens looking for a matching object property using reflection to 'bulk load' the tokens


you can call the SetToken method multiple times to make multiple object's data available


This is really powerful, as you no longer need specialized tokens built-in to the template engine but can pass any object with properties to the SetToken method, then use tokens to extract specific data values into your templates.

4. Render the output

The final step is to tell the template engine to 'process' the source, replacing the tokens with the values you've set. You can either call the Render() method with no parameters in which case the template engine will return a string, or you can pass a placeholder and the template engine will 'inject' all output into the placeholder. If your template will be rendering controls, or input controls, you have to use the placeholder method in order to inject actual control objects.

' render the template output to a string object
Dim results As String = template.ToString()
' render the template object into a placeholder
Dim ph As New PlaceHolder

If you want to build a list or table you can iterate through your data collection.

Dim results As String = String.Empty
For Each item In myItemCollection
  results = results & template.ToString()

The new template engine is extremely flexible and because it is being made visible as a public class, you will be able to easily use the template class in your own module development.

What's next?

We've talked about how to use the template engine. The next article will talk about 'tokens', what they are, how they're used and how you can extend them to include 'functionality' by writing your own token Render() function.


Comment Form

Only registered users may post comments.


2sic Daniel Mettler (125)
Aderson Oliveira (15)
Alec Whittington (11)
Alex Shirley (10)
Andrew Nurse (30)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (22)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (203)
Chris Paterra (55)
Clinton Patterson (28)
Cuong Dang (21)
Daniel Bartholomew (2)
Dave Buckner (2)
David Poindexter (3)
David Rodriguez (2)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (74)
Geoff Barlow (6)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (270)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matthias Schlomann (15)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Mike Horton (19)
Mitchel Sellers (28)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Peter Donker (52)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott S (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Timo Breumelhof (24)
Tony Henrich (3)
Torsten Weggen (2)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (37)
Will Strohl (163)
William Severance (5)
Try Evoq
For Free
Start Free Trial
a Demo
See Evoq Live
Need More Information?