Template Emplacement
.Place(parameterName, text, ...) creates a new template text where 'text' is embued into 'parameterName'.
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new ParameterlessText("a cat");
ITemplateText dog = new ParameterlessText("a dog");
// Embue "a cat and a dog"
ITemplateText catAndDog = and.Place("0", cat, "1", dog);
// Print
WriteLine(catAndDog.Print(null, null)); // "a cat and a dog"
Parameters are merged into the parent's ones. Separator '_' is used in the new parameter names.
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Embue 0=cat, 1=dog,
ITemplateText catsDogs = and.Place("0", cat, "1", dog);
// Create arguments
object?[] arguments = { 2, 3 };
// Print
string print = catsDogs.Print(null, arguments);
// Write
WriteLine(print); // "2 cat(s) and 3 dog(s)"
// Print the new composition
WriteLine(catsDogs); // "{0_count} cat(s) and {1_count}"
// Print parameters
WriteLine(string.Join(", ", catsDogs.ParameterNames)); // "0_count, 1_count"
The emplaced text uses same template format as the parent text. If the parent has numeric parameter names, e.g. TemplateFormat.BraceNumeric, then the new composition will also use numeric parameters.
// Create texts
ITemplateText ands = new TemplateText("{0}, {1} and {2}", TemplateFormat.BraceNumeric);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
ITemplateText pony = new TemplateText("{count} poni(es)", TemplateFormat.Brace);
// Embue "{0} cat(s), {1} dog(s) and {2} poni(es)"
ITemplateText catsDogsPonies = ands.Place("0", cat, "1", dog, "2", pony);
// Write parameters
WriteLine(string.Join(", ", catsDogsPonies.ParameterNames)); // "0, 1, 2"
If parent text has parameters that have format or alignment e.g. "{name,alignment:format}" then they are dropped. However, the alignment and format from emplacements will be used.
// Create texts
ITemplateText line = new TemplateText("{0,10}, {1,10}", TemplateFormat.Brace);
ITemplateText count = new TemplateText("Count = {count,-5}", TemplateFormat.Brace);
ITemplateText id = new TemplateText("Id = {id,-5}", TemplateFormat.Brace);
// Embue "Count = {0_count,-5}, Id = {1_id,-5}"
ITemplateText merge = line.Place("0", count, "1", id);
// Print
WriteLine(merge.Print(null, new object?[] { 1, 1 })); // "Count = 1 , Id = 1 "
ParameterlessText can be used for emplacing an argument permanently.
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Create "2 cat(s) and 3 dog(s)"
ITemplateText cat2 = cat.Place("count", new ParameterlessText("2"));
ITemplateText dog3 = dog.Place("count", new ParameterlessText("3"));
// Print
WriteLine(and.Print(null, new object?[] { cat2, dog3 })); // "2 cat(s) and 3 dog(s)"
TemplateTextAgglumation glues multiple texts into one. Optional 'glue' is added between texts.
// Create texts
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Create "2 cat(s) and 3 dog(s)"
ITemplateText agglumation = new TemplateTextAgglumation(
glue: " and ",
new TemplateParameterEmplacement("count", cat),
new TemplateParameterEmplacement("count", dog)
);
// Print
WriteLine(agglumation.Print(null, new object?[] { "1", "2" })); // "1 cat(s) and 2 dog(s)"
Full Example
Full example
using System.Globalization;
using Avalanche.Template;
using static System.Console;
public class templateemplacement
{
public static void Run()
{
{
// <01>
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new ParameterlessText("a cat");
ITemplateText dog = new ParameterlessText("a dog");
// Embue "a cat and a dog"
ITemplateText catAndDog = and.Place("0", cat, "1", dog);
// Print
WriteLine(catAndDog.Print(null, null)); // "a cat and a dog"
// </01>
}
{
// <02>
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Embue 0=cat, 1=dog,
ITemplateText catsDogs = and.Place("0", cat, "1", dog);
// Create arguments
object?[] arguments = { 2, 3 };
// Print
string print = catsDogs.Print(null, arguments);
// Write
WriteLine(print); // "2 cat(s) and 3 dog(s)"
// Print the new composition
WriteLine(catsDogs); // "{0_count} cat(s) and {1_count}"
// Print parameters
WriteLine(string.Join(", ", catsDogs.ParameterNames)); // "0_count, 1_count"
// </02>
}
{
// <03>
// Create texts
ITemplateText ands = new TemplateText("{0}, {1} and {2}", TemplateFormat.BraceNumeric);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
ITemplateText pony = new TemplateText("{count} poni(es)", TemplateFormat.Brace);
// Embue "{0} cat(s), {1} dog(s) and {2} poni(es)"
ITemplateText catsDogsPonies = ands.Place("0", cat, "1", dog, "2", pony);
// Write parameters
WriteLine(string.Join(", ", catsDogsPonies.ParameterNames)); // "0, 1, 2"
// </03>
}
{
// <04>
// Create texts
ITemplateText line = new TemplateText("{0,10}, {1,10}", TemplateFormat.Brace);
ITemplateText count = new TemplateText("Count = {count,-5}", TemplateFormat.Brace);
ITemplateText id = new TemplateText("Id = {id,-5}", TemplateFormat.Brace);
// Embue "Count = {0_count,-5}, Id = {1_id,-5}"
ITemplateText merge = line.Place("0", count, "1", id);
// Print
WriteLine(merge.Print(null, new object?[] { 1, 1 })); // "Count = 1 , Id = 1 "
// </04>
}
{
// <05>
// Create texts
ITemplateText and = new TemplateText("{0} and {1}", TemplateFormat.Brace);
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Create "2 cat(s) and 3 dog(s)"
ITemplateText cat2 = cat.Place("count", new ParameterlessText("2"));
ITemplateText dog3 = dog.Place("count", new ParameterlessText("3"));
// Print
WriteLine(and.Print(null, new object?[] { cat2, dog3 })); // "2 cat(s) and 3 dog(s)"
// </05>
}
{
// <11>
// Create texts
ITemplateText cat = new TemplateText("{count} cat(s)", TemplateFormat.Brace);
ITemplateText dog = new TemplateText("{count} dog(s)", TemplateFormat.Brace);
// Create "2 cat(s) and 3 dog(s)"
ITemplateText agglumation = new TemplateTextAgglumation(
glue: " and ",
new TemplateParameterEmplacement("count", cat),
new TemplateParameterEmplacement("count", dog)
);
// Print
WriteLine(agglumation.Print(null, new object?[] { "1", "2" })); // "1 cat(s) and 2 dog(s)"
// </11>
}
}
}