在ST4/ c#中,你可以注册一个没有TemplateGroup的新渲染器吗?
本文关键字:一个 TemplateGroup 新渲染 ST4 注册 | 更新日期: 2023-09-27 18:17:55
RegisterRenderer方法出现的唯一地方是在TemplateGroup。但是我只有一个模板,通过字符串提供,而不是文件系统上的多个模板。
或者,我如何使用TemplateGroup,但通过字符串提供模板?
嗯,我弄清楚了如何从字符串中做模板组(这里的文档不是很好,特别是对于c#端口)。虽然不是解决实际问题的办法,但这是一个变通办法,因为我可以在组中注册自定义渲染器。
实际上,我正在为单个模板创建一个模板组,这看起来很傻,但是无论如何:
// Create the group (I'm specifying custom delimiters, but you don't have to)
var group = new TemplateGroup('$','$');
// Here's 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's still gonna get run through the renderer (very important with strings, for instance)
group.RegisterRenderer(typeof(DateTime), new DateRenderer());
//Here's 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();
RegisterRenderer()
方法可通过Group
-Property:
Dim tmpl As New Template(s, "$", "$")
tmpl.Group.RegisterRenderer(GetType(DateTime), New MyDateRenderer())