Text Templates in AngelScript

import std.string;
import std.stream;

void main() {
    // We can create a new template and parse its body from
    // a string.
    // Templates are a mix of static text and "actions" enclosed in
    // {{...}} that are used to dynamically insert content.
    string t1 = "Value is {{.}}\n";

    // By "executing" the template we generate its text with
    // specific values for its actions. The {{.}} action is
    // replaced by the value passed as a parameter to Execute.
    print(string::format(t1, "some text"));
    print(string::format(t1, 5));
    print(string::format(t1, ["Go", "Rust", "C++", "C#"]));

    // If the data is a dictionary we can use the {{.FieldName}} action to access
    // its fields.
    string t2 = "Name: {{.Name}}\n";
    dictionary person;
    person["Name"] = "Jane Doe";
    print(string::format(t2, person));

    // The same applies to dictionaries; with dictionaries there is no restriction on the
    // case of key names.
    dictionary character;
    character["Name"] = "Mickey Mouse";
    print(string::format(t2, character));

    // if/else provide conditional execution for templates. A value is considered
    // false if it's the default value of a type, such as 0, an empty string,
    // null, etc.
    string t3 = "{{if .}}yes{{else}}no{{end}}\n";
    print(string::format(t3, "not empty"));
    print(string::format(t3, ""));

    // range blocks let us loop through arrays or dictionaries. Inside
    // the range block {{.}} is set to the current item of the iteration.
    string t4 = "Range: {{#each .}}{{.}} {{/each}}\n";
    print(string::format(t4, ["Go", "Rust", "C++", "C#"]));
}

This AngelScript code demonstrates the concept of text templates, which is similar to the Go example. However, AngelScript doesn’t have a built-in templating system like Go’s text/template package. Instead, we’re using string formatting and a hypothetical templating syntax that’s similar to Handlebars or Mustache.

Here’s a breakdown of the changes and explanations:

  1. We import std.string and std.stream for string manipulation and output operations.

  2. Instead of creating template objects, we define template strings directly.

  3. We use string::format() to simulate template execution. In a real AngelScript application, you’d likely use a third-party templating library or implement your own.

  4. Structs in the Go example are replaced with dictionaries in AngelScript.

  5. The range construct in Go templates is simulated using a {{#each}} block in our hypothetical template syntax.

  6. Error handling is omitted for simplicity, but in a real application, you’d want to add appropriate error checking.

  7. The output is printed directly to the console using the print() function.

This example demonstrates how to achieve similar functionality to Go’s text templates in AngelScript, although the exact implementation would depend on the specific templating library or custom implementation you choose to use.