Text Templates in Objective-C

Our first program will demonstrate how to use text templates in Objective-C. Here’s the full source code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // 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.
        NSString *template1 = @"Value is {{value}}\n";
        
        // In Objective-C, we don't have a built-in template engine like Go's text/template.
        // We'll use a simple string replacement method to simulate template behavior.
        NSString *result1 = [template1 stringByReplacingOccurrencesOfString:@"{{value}}" withString:@"some text"];
        NSLog(@"%@", result1);
        
        result1 = [template1 stringByReplacingOccurrencesOfString:@"{{value}}" withString:@"5"];
        NSLog(@"%@", result1);
        
        NSArray *languages = @[@"Objective-C", @"Swift", @"C++", @"C#"];
        result1 = [template1 stringByReplacingOccurrencesOfString:@"{{value}}" withString:[languages description]];
        NSLog(@"%@", result1);
        
        // If the data is a dictionary we can use the key to access its values.
        NSString *template2 = @"Name: {{name}}\n";
        NSDictionary *person = @{@"name": @"Jane Doe"};
        NSString *result2 = [template2 stringByReplacingOccurrencesOfString:@"{{name}}" withString:person[@"name"]];
        NSLog(@"%@", result2);
        
        // Conditional execution can be simulated using if statements
        NSString *template3 = @"{{if_not_empty}} yes {{else}} no {{endif}}\n";
        NSString *value = @"not empty";
        NSString *result3 = [template3 stringByReplacingOccurrencesOfString:@"{{if_not_empty}}" withString:(value.length > 0 ? @"" : @"{{else}}")];
        result3 = [result3 stringByReplacingOccurrencesOfString:@"{{else}} no {{endif}}" withString:@""];
        NSLog(@"%@", result3);
        
        value = @"";
        result3 = [template3 stringByReplacingOccurrencesOfString:@"{{if_not_empty}}" withString:(value.length > 0 ? @"" : @"{{else}}")];
        result3 = [result3 stringByReplacingOccurrencesOfString:@"{{else}}" withString:@""];
        result3 = [result3 stringByReplacingOccurrencesOfString:@"{{endif}}" withString:@""];
        NSLog(@"%@", result3);
        
        // Range blocks can be simulated using a loop
        NSString *template4 = @"Range: {{range}}\n";
        NSMutableString *rangeResult = [NSMutableString string];
        for (NSString *lang in languages) {
            [rangeResult appendFormat:@"%@ ", lang];
        }
        NSString *result4 = [template4 stringByReplacingOccurrencesOfString:@"{{range}}" withString:rangeResult];
        NSLog(@"%@", result4);
    }
    return 0;
}

To run the program, save the code in a file named TextTemplates.m and compile it using:

$ clang -framework Foundation TextTemplates.m -o TextTemplates
$ ./TextTemplates
Value is some text
Value is 5
Value is (
    "Objective-C",
    Swift,
    "C++",
    "C#"
)
Name: Jane Doe
yes 
no 
Range: Objective-C Swift C++ C# 

This example demonstrates how to simulate text templates in Objective-C. Since Objective-C doesn’t have a built-in template engine like Go’s text/template, we use string replacements to achieve similar functionality. The concepts of variable substitution, conditional execution, and range iteration are simulated using Objective-C’s string manipulation methods and control structures.

Note that this is a basic implementation and doesn’t provide all the features of a full-fledged template engine. For more complex template needs in Objective-C projects, you might want to consider using third-party libraries or implementing a more sophisticated template system.