65.9K
CodeProject is changing. Read more.
Home

Conditional Replacement

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.09/5 (6 votes)

Sep 20, 2005

3 min read

viewsIcon

35991

downloadIcon

375

Templating for text based formats.

Sample Image

Introduction

Conditional Replacement is used to merge data in an object model with text based templates.

It uses Reflection to read the properties and methods of your data object and recursively merges the data with the template using conditional placeholders, e.g., Welcome [Name] to the Code Project. Here the Name place holder would be replaced with the data in your object on its 'Name' property.

Background

Most IT projects require the production of a formatted version of the data the system is manipulating, whether it be CSV/Excel/Word documents for reports and invoicing, XML for web services, HTML pages for a content management system, etc.

CRep was developed to enable the creation of a variety of text based documents in a structured, re-usable and simple to read manner.

The Demo Project

This contains a simple object model for the RSS feed available on this site. It loads the data into the object model and generates an HTML file from the data and the template provided. You will require the .NET framework 1.1.

Using the code

When creating a ConditionalReplacement object you are required to pass in the template as a string.

You will then be able to generate your result by calling the Replace function with your data object.

// Conditional Replacement Object
// Initialized with the template
ConditionalReplacement cRep = 
  new ConditionalReplacement( "Welcome [Name] to the Code Project" );

// Replace and write to output file
string output = cRep.Replace( UserDataObject );

Syntax for Templates

CRep looks for placeholders in the template, these are marked using the following tokens:

Token Description
[ Open place holder
] Close place holder
| General Separator
@ Collection Indicator
# Format indicator
\ Token Switch

Non-conditional

Simple replacement of placeholders with data; you can access sub object properties using the standard 'dot' notation.

[<propertyName>]

E.g. Hello [Person.Forename]

-> Hello Anthony

*N.B. If you find you need to use a token in the template and want CRep to ignore it, just apply the switch '\' before it.

E.g. Hello \[[Person.Forename]\]

-> Hello [Anthony]

Conditional

Replacement of placeholders based on the data in the property; you can have as many conditions as you require.

[<propertyName>|=<condition>|<result>|~|<defaultResult>]

E.g. Hello [Person.Forname] ([Person.Forname|=Anthony|great|~|nice] name)

If the person's forename is 'Anthony':

-> Hello Anthony (great name)

Otherwise:

-> Hello Sarah (nice name)

Recurse Sub-Object

You can recurse to sub objects. Think of it as an embedded template using the sub object as its data.

[<propertyName>|..[<subObjectPropertyName>]..]

E.g. Hello [Person|[Forname] [Surname]]

-> Hello Anthony Johnston

Recurse Sub-Collection

You can recurse the objects in sub collections too. Each object will be subject to the 'embedded template'.

[<propertyName>@..[<subObjectPropertyName>]..]

E.g. Hello [People@[Person.Forname], ]

-> Hello Anthony, Sarah,

Formatting Data

Data can be formatted, this uses the standard String.Format() method. More details on this method can be found on the Microsoft web site.

[<propertyName>#<format>]

E.g. Your birth date was [Person.DateOfBirth#d MMM yyyy]

-> Your birth date was 1 Dec 1970

Regular Expressions

Some may ask why it doesn't use regular expressions, there are three compelling reasons;

  1. Regular expressions don't handle nested placeholders.
  2. Regular expressions are difficult to read and understand.
  3. Regular expressions are slow.

History

  • 1.0 - 6 September 2005.