Click here to Skip to main content
Click here to Skip to main content

Conditional Replacement

, 20 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Anthony Johnston
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralGood work PinmemberMagomed Abdurakhmanov18-Sep-08 9:35 
Thanks for sharing.
 
Cheers,
Magomed Abdurakhmanov

GeneralFile naming Pinmemberfwsouthern20-Sep-05 18:08 
GeneralRe: File naming PinmemberAnthony Johnston28-Sep-05 10:42 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 20 Sep 2005
Article Copyright 2005 by Anthony Johnston
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid