Add your own extension or helper utility classes to use during template generation in lpt file

Posts   
 
    
SG1991
User
Posts: 2
Joined: 26-Dec-2024
# Posted on: 26-Dec-2024 10:44:55   

Hello,

I was wondering if it's possible to add your own extension or helper utility classes to use in template generation like the GeneratorUtils.CamelCaseString().

To illustrate my use case, I have a lpt template where I would like to use kebab-case for urls. Either through an extension class like this:

    public static class StringExtensions
    {
        public static string ToKebabCase(this string text)
        {
            return Regex.Replace(text, "(?<!^)(?<!/)([A-Z][a-z]|(?<=[a-z])[A-Z0-9])", "-$1", RegexOptions.Compiled).Trim().ToLower();
        }
    }

So that in my template I can do:

        <% foreach(var entityToProcess in entitiesToProcess){ %> 
        public const string <%=entityToProcess.Name%> = "<%=entityToProcess.Name.ToKebabCase()%>";
        <%}%>

or through a general utility class like the GeneratorUtils.CamelCaseString()

Is this something that's possible to do?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 27-Dec-2024 09:28:17   

Please see: https://www.llblgen.com/Documentation/5.11/SDK/templates_lpt.htm#assembly-reference-declarations Which describes what to do to have the templates load an assembly at runtime (the designer is a .net framework app so either compile your assembly against netstandard20 or .net 4.8 )

However a small function like that, you can also create a template for that and refer to the function in other templates. For instance our templates for e.g. entity framework core have a generalTemplateUtils.lpt template, bound to the id SD_EF_GeneralUtils in the templatebindings file SD.EntityFramework.Shared

The functions used in that template are then used like string providerName = SD_EF_GeneralUtils.DriverIdToProviderName(driverID); as the functions in a template within a <~ ~> block are compiled into a class named after the id the template is bound to (in this case SD_EF_GeneralUtils).

Frans Bouma | Lead developer LLBLGen Pro
SG1991
User
Posts: 2
Joined: 26-Dec-2024
# Posted on: 27-Dec-2024 13:42:19   

Otis wrote:

Please see: https://www.llblgen.com/Documentation/5.11/SDK/templates_lpt.htm#assembly-reference-declarations Which describes what to do to have the templates load an assembly at runtime (the designer is a .net framework app so either compile your assembly against netstandard20 or .net 4.8 )

However a small function like that, you can also create a template for that and refer to the function in other templates. For instance our templates for e.g. entity framework core have a generalTemplateUtils.lpt template, bound to the id SD_EF_GeneralUtils in the templatebindings file SD.EntityFramework.Shared

The functions used in that template are then used like string providerName = SD_EF_GeneralUtils.DriverIdToProviderName(driverID); as the functions in a template within a <~ ~> block are compiled into a class named after the id the template is bound to (in this case SD_EF_GeneralUtils).

Thanks!

Got it working by making a utility template with the method in it as you suggested.

<[System.Text.RegularExpressions]>
<~
public static string ToKebabCase(string text)
{
    return Regex.Replace(text, "(?<!^)(?<!/)([A-Z][a-z]|(?<=[a-z])[A-Z0-9])", "-$1", RegexOptions.Compiled).Trim().ToLower();
}
~>
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 28-Dec-2024 08:16:43   

Great! simple_smile

Frans Bouma | Lead developer LLBLGen Pro