Click here to Skip to main content
13,052,870 members (62,420 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 22 Oct 2013

Simple templating engine

, 23 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Using reflection to build a templating engine


A simple micro templating engine that uses data objects and reflection to effectively populate a template.


On several occasions I have come across the need for a tiny and efficient yet simple templating engine to print reports, articles, snippets etc within larger systems where a full blown CMS is not warranted. Hence I came up with this solution where you can populate an object and pass it with a template  to a template engine that fleshes out the template. This has worked quite well for me and I would like to share it with any one who has a similar need.

Using the code

The idea behind the templating engine is to use a data object to populate a template. The properties themselves will be markers in the template and the values of these properties will flesh out  the template.

At the heart of the engine is the below method that will use reflection to iterate through the properties and replace the tags/markers in the template.

public string MergeObject(object MergeObject, string tempDoc)
    string result = tempDoc;
    Type t = MergeObject.GetType();
    PropertyInfo[] props = t.GetProperties();
    foreach (PropertyInfo prp in props)
        if (MergeObject != null)
           result = Regex.Replace(result, "<"+prp.Name+">", 
                             prp.GetValue(MergeObject, null).ToString());                     
    return result;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ritesh Ramesh
Architect Infosolvex Solutions Inc
Australia Australia
Ritesh is an IT consultant with over ten years of experience in the IT industry varying from consultation, architecture, design, development to technical management. He has a strong background in solutions and applications architecture with a focus on Microsoft’s .Net platform. His area of expertise spans design and implementation of client/server, database and web-based systems. He has worked with C#, ASP.NET 1.1 and 2.0, ADO.NET, Web Services and SQL technology on several enterprise class projects.

Freedom is not worth having if it does not include the freedom to make mistakes.
Mahatma Gandhi

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Maykonn Welington Candido23-Oct-13 9:47
memberMaykonn Welington Candido23-Oct-13 9:47 
GeneralMy vote of 2 Pin
John Brett23-Oct-13 5:41
memberJohn Brett23-Oct-13 5:41 
GeneralMy vote of 1 Pin
Mark Lemke23-Oct-13 3:27
memberMark Lemke23-Oct-13 3:27 
Questionsuggestion for improvement Pin
Member 1008959523-Oct-13 2:44
memberMember 1008959523-Oct-13 2:44 
GeneralMy vote of 1 Pin
Cool Dirty Boy23-Oct-13 1:32
memberCool Dirty Boy23-Oct-13 1:32 
QuestionTemplating Engine? Pin
Nitij Kumar23-Oct-13 1:15
professionalNitij Kumar23-Oct-13 1:15 
GeneralMy vote of 1 Pin
Pete O'Hanlon23-Oct-13 0:48
protectorPete O'Hanlon23-Oct-13 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Oct 2013
Article Copyright 2013 by Ritesh Ramesh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid