In ST4 / C #, can you register a new render without TemplateGroup?

The only place where the RegisterRenderer method appears is in the TemplateGroup. But I only have one template supplied via string, not multiple templates on the filesystem.

Alternatively, how can I use TemplateGroup, but supply the template via a string?

+3


source to share


2 answers


Well I figured out how to make groups of templates from strings (the documentation here is not very good, especially for the C # port). While not a solution to the real problem, it is a workaround since I can register my own renderers with the group.

Basically, I'm creating a group of templates for one template, which seems silly, but whatever:



// Create the group (I'm specifying custom delimiters, but you don't have to)
var group = new TemplateGroup('$','$');

// Here where we bind the renderers to a type. They will run on EVERY occurrence of this type, which means you have to handle situations in your renderer where no format string was specified -- that still gonna get run through the renderer (very important with strings, for instance)
group.RegisterRenderer(typeof(DateTime), new DateRenderer());

//Here where you "register" (define) a template. You have to give it a name, and (weirdly) specify all the attributes you're going to use
group.DefineTemplate("default", "[template code here]", new string[] { "nameOfAttribute" });
// You can define more templates here...

// Now, get the template back out using the name you used before
var template = group.GetInstanceOf("default");

// Add the data (correctly using the names specified when you defined the template above)
template.Add("nameOfAttribute", my attribute);

// Render
var result = template.Render();

      

+1


source


The method RegisterRenderer()

is available via Group

-Property:



Dim tmpl As New Template(s, "$", "$")
tmpl.Group.RegisterRenderer(GetType(DateTime), New MyDateRenderer())

      

0


source







All Articles