I'm trying to call a subtemplate where I nedd to pass two expressions as parameter and deal with those parameter with a CustomRenderer.
When calling a subtemplate + CustomRenderer with one parameter it's done by calling a method from the template context as code below.
My question is: how can I call for example
$concat2values(context.Value, "Literal")$
given context.Value is a string "somevalue" and as a result receive
somevalueLiteral
obs.: my concerns are that the CustomRenderer just reveice one object paramater and in no try I could build the template this way. My aim is to intercep some processed code from script back on the "Engine" that process it.
templategroup.stg
group mygroup;
delimiters "$","$"
main(context) ::= <<
$formatSomething(context.Value)$
>>
formatSomething(value) ::= <<
$value; format = "_formatSomething_"$
>>
myformater.cs
using Antlr4.StringTemplate;
using System;
using System.Globalization;
using System.Linq;
namespace Engine.Components.Engine.Formatters
{
public class SomeFormatter: StringRenderer
{
public override string ToString(object o, string formatString, CultureInfo culture)
{
if ("_formatSomething_".Equals(formatString))
return FormatSomething(o);
return base.ToString(o, formatString, culture);
}
private string FormatSomething(object o)
{
** DO STUFF **
string result = do_stuff_and_return_it_formatted();
return result;
}
}
}
mytemplateengine.cs
private string ProcessTemplate<T>(string fullPathTemplateFileName, T context)
{
try
{
var templateSource = File.ReadAllText(fullPathTemplateFileName);
var groupTemplate = CreateTemplateGroup(templateSource);
var templates = new TemplateGroupString(groupTemplate);
// vvvvvvv----- SHOULD I USE OBJECT OR ANOTHER SPECIFIC TYPE TO THE TASK?
templates.RegisterRenderer(typeof(object), new SomeFormatter());
var engine = templates.GetInstanceOf("main");
if (engine == null)
throw new Exception($"Failed to get template {fullPathTemplateFileName}");
engine.Add("context", context);
return engine.Render();
}
catch (Exception ex)
{
Errors.Add($"{ex.Message}{ex.StackTrace}");
return $"{ex.Message}{ex.StackTrace}";
}
}