Text Templates in UnrealScript

class TextTemplates extends Object;

struct NameStruct
{
    var string Name;
};

function Main()
{
    local string Template1, Template2, Template3, Template4;
    local array<string> Languages;
    local NameStruct PersonStruct;
    local Map<string, string> PersonMap;

    // UnrealScript doesn't have built-in text template support,
    // so we'll simulate it using string operations

    // Simple template
    Template1 = "Value is {0}";
    `log(ReplaceText(Template1, "{0}", "some text"));
    `log(ReplaceText(Template1, "{0}", string(5)));

    Languages.AddItem("UnrealScript");
    Languages.AddItem("C++");
    Languages.AddItem("Java");
    Languages.AddItem("Python");
    `log(ReplaceText(Template1, "{0}", string(Languages)));

    // Template with struct
    Template2 = "Name: {0}";
    PersonStruct.Name = "Jane Doe";
    `log(ReplaceText(Template2, "{0}", PersonStruct.Name));

    // Template with map
    PersonMap.Set("Name", "Mickey Mouse");
    `log(ReplaceText(Template2, "{0}", PersonMap.Get("Name")));

    // Conditional template
    Template3 = "{{if .}} yes {{else}} no {{end}}";
    `log(ConditionalTemplate(Template3, "not empty"));
    `log(ConditionalTemplate(Template3, ""));

    // Range template
    Template4 = "Range: {0}";
    `log(ReplaceText(Template4, "{0}", JoinArray(Languages, " ")));
}

function string ConditionalTemplate(string Template, string Value)
{
    if (Len(Value) > 0)
    {
        return ReplaceText(Template, "{{if .}} yes {{else}} no {{end}}", "yes");
    }
    else
    {
        return ReplaceText(Template, "{{if .}} yes {{else}} no {{end}}", "no");
    }
}

function string JoinArray(array<string> Arr, string Separator)
{
    local int i;
    local string Result;

    for (i = 0; i < Arr.Length; i++)
    {
        if (i > 0)
        {
            Result $= Separator;
        }
        Result $= Arr[i];
    }

    return Result;
}

DefaultProperties
{
}

This UnrealScript code simulates the functionality of text templates as shown in the original example. Here’s an explanation of the key differences and adaptations:

  1. UnrealScript doesn’t have built-in support for text templates, so we use string operations to simulate this functionality.

  2. The template.New() and template.Parse() operations are replaced with simple string assignments.

  3. Instead of using {{.}} for template placeholders, we use {0} and the ReplaceText() function to substitute values.

  4. Structs and maps are used similarly to the original example, but we access their values directly in UnrealScript.

  5. The conditional template is implemented using a separate function ConditionalTemplate() that checks if the value is empty.

  6. The range functionality is simulated using a JoinArray() function that concatenates array elements with a separator.

  7. UnrealScript uses log instead of fmt.Println for output. In this example, we use the log statement.

  8. Error handling is not implemented in this example, as UnrealScript doesn’t have a direct equivalent to Go’s panic() function.

This code demonstrates how to achieve similar functionality to Go’s text templates in UnrealScript, despite the language differences. The core concepts of string manipulation, conditional logic, and iterating over collections are preserved.