Text Templates in ActionScript

ActionScript doesn’t have a built-in templating system like Go’s text/template. However, we can simulate a simple templating system using string interpolation and custom functions. Here’s an example of how we might approach this:

package {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class TextTemplates extends Sprite {
        public function TextTemplates() {
            // We can create a new "template" using a function that returns a string
            // with placeholders for dynamic content
            function createTemplate(template:String):Function {
                return function(value:*):String {
                    return template.replace(/\{\{.*?\}\}/g, String(value));
                }
            }

            // Create a simple template
            var t1:Function = createTemplate("Value is {{}}");

            // Execute the template with different values
            trace(t1("some text"));
            trace(t1(5));
            trace(t1(["ActionScript", "JavaScript", "TypeScript", "Haxe"]));

            // Helper function to create and execute a template in one step
            function executeTemplate(template:String, value:*):String {
                return createTemplate(template)(value);
            }

            // If the data is an object, we can use custom placeholders
            var t2:String = executeTemplate("Name: {{name}}", {name: "Jane Doe"});
            trace(t2);

            // The same applies to XML; with XML we can use E4X syntax
            var person:XML = <person><name>Mickey Mouse</name></person>;
            trace(executeTemplate("Name: {{name}}", person.name));

            // Conditional execution can be simulated using a ternary operator
            function conditionalTemplate(value:*):String {
                return value ? "yes" : "no";
            }
            trace(conditionalTemplate("not empty"));
            trace(conditionalTemplate(""));

            // Looping through arrays can be done using Array.map()
            var languages:Array = ["ActionScript", "JavaScript", "TypeScript", "Haxe"];
            trace("Range: " + languages.map(function(item:String):String {
                return item + " ";
            }).join(""));
        }
    }
}

To run this ActionScript code, you would typically compile it into a SWF file and run it in a Flash Player or AIR runtime environment. The output would be displayed in the trace console or a TextField, depending on your setup.

This example demonstrates how to create simple text templates in ActionScript, including:

  1. Creating and executing templates with placeholders.
  2. Using templates with different data types (strings, numbers, arrays).
  3. Accessing object properties in templates.
  4. Simulating conditional execution.
  5. Iterating over arrays to generate output.

While this approach doesn’t offer all the features of Go’s text/template package, it provides a basic way to generate dynamic content in ActionScript. For more complex templating needs, you might consider using a third-party library or developing a more comprehensive custom solution.