I was more looking for back end transformation to get a string back, however i found a work around that is working quite nicely, i'll share!
This uses MacroResolvers, which basically you can put your IDataClass as a Macro Resolver and then feed the "Template" to the MacroResolver to render. I stored the template strings in a database that the customer (or myself) can modify without needing to touch back end code.
Step 1: Create the Macro Resolver, filling it with the IDataClass sets
public static ContextResolver getContextResolver(int ItemID, int OtherItemID)
{
ContextResolver resolver = CMSContext.CurrentResolver.CreateContextChild();
// Get any IDataClass sets you want to "Load" into your resolver
IDataClass MyDatabaseObject = Custom.GetTheObject(ItemID);
IDataClass MyOtherDatabaseObject = Custom.GetTheOtherObject(OtherItemID)
// set the macro callers up with the object data
resolver.SetNamedSourceData("MainInfo", MyDatabaseObject);
resolver.SetNamedSourceData("OtherInfo", MyOtherDatabaseObject);
// Now if a macro is passed to the resolver, like "Hello {% MainInfo.Name %}" it will replace the
// MainInfo.Name with the Name column of that object loaded into it, in this case the MyDatabaseObject
return resolver;
}
Step 2: Grab the template string, i did from a database that has Name, Template
public static string getOtherTemplate(ref ContextResolver objectResolver, string templateName)
{
// Fill the resolver with custom data
// resolver.SourceParameters = new object[,] { { "MySimpleValue", "RESOLVED Simple value!" }, { "MySecondSimpleValue", "RESOLVED Second simple value!" } };
// Use the resolver to resolve macros in text
return objectResolver.ResolveMacros(getOtherTemplateMacro(templateName));
}
public static string getOtherTemplateMacro(string templateName)
{
QueryDataParameters parameters = new QueryDataParameters();
parameters.Add("@TemplateName", templateName);
DataSet ds = SqlHelperClass.ExecuteQuery("custom.templates.SelectTemplateByName", parameters, null, null);
if (ds.Tables[0].Rows.Count == 0) return "No template found with name " + templateName;
else return ds.Tables[0].Rows[0]["Template"].ToString();
}
To execute, simply use the following then
ContextResolver myResolver = Custom.getResolver(1, 2);
String results = Custom.getOtherTemplate(ref MyResolver, "SomeTemplateNameThatMatchesTheNameColumnInDatabase");
Then just use K# syntax in the Database template field and you're set! Works quite well actually...