TemplateLib 1.0.0

dotnet add package TemplateLib --version 1.0.0
NuGet\Install-Package TemplateLib -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TemplateLib" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TemplateLib --version 1.0.0
#r "nuget: TemplateLib, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TemplateLib as a Cake Addin
#addin nuget:?package=TemplateLib&version=1.0.0

// Install TemplateLib as a Cake Tool
#tool nuget:?package=TemplateLib&version=1.0.0

Message template sdk Nuget GitHub release (latest by date)

Overview

A small library for creating templates from blocks using code. The same blocks can be used for different templates. The basic principle of working with the library is to provide a flexible and simple system for writing dynamically generated text.

Writing a template with variables

The writer's regex is passed a template with selectors, a regex string to find these selectors, and a lambda function to create selectors.

var writer = new RegexTextWriter(
    "I hold %[ITEM]% in my %[HAND]%",
    "%\\[([^%,\\s]+)\\]%",
    s => $"%[{s}]%"
);
var text = writer.ToWriting(new Dictionary<string, string>
{
    {"ITEM", "tea"},
    {"HAND", "left hand"}
});
Assert.AreEqual("I hold tea in my left hand", text);

If you do not pass a value to any variable, it is replaced with a default value, which can also be passed (by default, this is an empty string).

Generating template from blocks

Using TemplateBlockFactory for creating blocks with default regex.

Order example

var deliveryConfiguration = TextBlockFactory.CreateTemplate(
    "Delivery method: %[METHOD]%\n" +
    "Delivery address: %[ADDRESS]%\n" +
    "Delivery date: %[DATE]%",
    new Dictionary<string, ITextBlock>
    {
        {"METHOD", TextBlockFactory.CreateText("courier")},
        {"ADDRESS", TextBlockFactory.CreateText("Khreschatyk St, 32, Kyiv, 02000")},
        {"DATE", TextBlockFactory.CreateText("08.04.2021")}
    }
);
var paymentConfiguration = TextBlockFactory.CreateTemplate(
    "Payment method: %[METHOD]%",
    new Dictionary<string, ITextBlock>
    {
        {"METHOD", TextBlockFactory.CreateText("cash")}
    }
);
var orderConfiguration = TextBlockFactory.CreateTemplate(
    "Customer name: %[CUSTOMER_NAME]%\n" +
    "Phone: %[PHONE]%\n" +
    "Email address: %[EMAIL]%\n" +
    "%[DELIVERY_CONFIGURATION]%\n" +
    "%[PAYMENT_CONFIGURATION]%\n" +
    "Comment to order: %[COMMENT]%\n" +
    "Order date: %[ORDER_DATE]%",
    new Dictionary<string, ITextBlock>
    {
        {"CUSTOMER_NAME", TextBlockFactory.CreateText("Vladislav Shirokiy")},
        {"PHONE", TextBlockFactory.CreateText("+8888888888")},
        {"EMAIL", TextBlockFactory.CreateText("vlad16062001@gmail.com")},
        {"DELIVERY_CONFIGURATION", deliveryConfiguration},
        {"PAYMENT_CONFIGURATION", paymentConfiguration},
        {"COMMENT", TextBlockFactory.CreateText("Deliver quickly!")},
        {"ORDER_DATE", TextBlockFactory.CreateText("04.08.2021")}
    }
);
var orderFactory = new Func<int, (string, int, int), ITextBlock>((number, tuple) =>
    TextBlockFactory.CreateTemplate("%[NUMBER]%. %[ITEM]% %[COUNT]%x%[PRICE]% $",
        new Dictionary<string, ITextBlock>
        {
            {"NUMBER", TextBlockFactory.CreateText(number.ToString())},
            {"ITEM", TextBlockFactory.CreateText(tuple.Item1)},
            {"COUNT", TextBlockFactory.CreateText(tuple.Item2.ToString())},
            {"PRICE", TextBlockFactory.CreateText(tuple.Item3.ToString())},
        }
    )
);
var orders = new List<(string, int, int)>
{
    ("Chair", 1, 25),
    ("Table", 1, 50),
    ("Wardrobe", 1, 45)
};
const int discount = 15;
const int shippingCost = 25;
var block = TextBlockFactory.CreateTemplate(
    "Order №%[ORDER_NUMBER]%\n" +
    "%[ORDER_CONFIGURATION]%\n" +
    "-------\n" +
    "%[ORDERS]%\n" +
    "\n" +
    "%[CHECK]%",
    new Dictionary<string, ITextBlock>
    {
        {"ORDER_NUMBER", TextBlockFactory.CreateText("13")},
        {"ORDER_CONFIGURATION", orderConfiguration},
        {
            "ORDERS", TextBlockFactory.MergeTemplates("\n", orders
                .Select((order, number) => orderFactory(number + 1, order))
                .ToArray()
            )
        },
        {
            "CHECK", TextBlockFactory.CreateTemplate(
                "Discount: %[DISCOUNT]% $\n" +
                "Shipping cost: %[SHIPPING_COST]% $\n" +
                "Total amount: %[TOTAL_AMOUNT]% $",
                new Dictionary<string, ITextBlock>
                {
                    {"DISCOUNT", TextBlockFactory.CreateText(discount.ToString())},
                    {"SHIPPING_COST", TextBlockFactory.CreateText(shippingCost.ToString())},
                    {
                        "TOTAL_AMOUNT", TextBlockFactory.CreateText(
                            (orders.Aggregate(0,
                                    (total, order) => total + order.Item2 * order.Item3) +
                                shippingCost -
                                discount)
                            .ToString()
                        )
                    }
                }
            )
        }
    }
);
const string result = "Order №13\n" +
    "Customer name: Vladislav Shirokiy\n" +
    "Phone: +8888888888\n" +
    "Email address: vlad16062001@gmail.com\n" +
    "Delivery method: courier\n" +
    "Delivery address: Khreschatyk St, 32, Kyiv, 02000\n" +
    "Delivery date: 08.04.2021\n" +
    "Payment method: cash\n" +
    "Comment to order: Deliver quickly!\n" +
    "Order date: 04.08.2021\n" +
    "-------\n" +
    "1. Chair 1x25 $\n" +
    "2. Table 1x50 $\n" +
    "3. Wardrobe 1x45 $\n" +
    "\n" +
    "Discount: 15 $\n" +
    "Shipping cost: 25 $\n" +
    "Total amount: 130 $";
Assert.AreEqual(block.Write(), result);

How add package to your project?

Add package Reference

<PackageReference Include="TemplateLib" Version="1.0.0" />
Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 775 8/14/2021