Text Templates in C

Our first program demonstrates the use of text templates in C. Here’s the full source code:

#include <stdio.h>
#include <string.h>

// A simple template structure
typedef struct {
    char *name;
    char *content;
} Template;

// Function to create a new template
Template* create_template(const char *name, const char *content) {
    Template *t = malloc(sizeof(Template));
    t->name = strdup(name);
    t->content = strdup(content);
    return t;
}

// Function to execute a template
void execute_template(Template *t, void *data) {
    // This is a very simplified version. In a real implementation,
    // you would parse the template and replace placeholders.
    printf(t->content, data);
}

int main() {
    // Create a simple template
    Template *t1 = create_template("t1", "Value is %s\n");

    // Execute the template with different types of data
    execute_template(t1, "some text");
    
    char number[20];
    sprintf(number, "%d", 5);
    execute_template(t1, number);

    // Create a template for struct data
    Template *t2 = create_template("t2", "Name: %s\n");

    // Define a struct and execute the template
    struct Person {
        char name[50];
    };
    struct Person jane = {"Jane Doe"};
    execute_template(t2, jane.name);

    // Create a template for conditional execution
    Template *t3 = create_template("t3", "Is not empty: %s\n");
    execute_template(t3, "yes");
    execute_template(t3, "no");

    // Create a template for iteration
    Template *t4 = create_template("t4", "Range: %s\n");
    char *languages[] = {"C", "C++", "Rust", "Go"};
    char range[100] = "";
    for (int i = 0; i < 4; i++) {
        strcat(range, languages[i]);
        strcat(range, " ");
    }
    execute_template(t4, range);

    // Free allocated memory
    free(t1->name); free(t1->content); free(t1);
    free(t2->name); free(t2->content); free(t2);
    free(t3->name); free(t3->content); free(t3);
    free(t4->name); free(t4->content); free(t4);

    return 0;
}

This C code demonstrates a very basic implementation of text templates. Unlike the original example, C doesn’t have built-in support for templates, so we’ve created a simple structure and functions to mimic the behavior.

We define a Template structure and functions to create and execute templates. The execute_template function is greatly simplified and doesn’t actually parse the template - it just uses printf to format the string.

We create templates for different purposes:

  1. A simple value template
  2. A template for struct data
  3. A template for conditional execution (simplified)
  4. A template for iteration (simplified)

Note that this is a very basic implementation and doesn’t include features like actual parsing of template syntax, proper error handling, or memory management that you’d want in a production-ready template system.

To compile and run this program:

$ gcc -o templates templates.c
$ ./templates
Value is some text
Value is 5
Name: Jane Doe
Is not empty: yes
Is not empty: no
Range: C C++ Rust Go 

This example demonstrates basic string manipulation and function usage in C. While it doesn’t have the same power as Go’s template package, it shows how you might start to implement similar functionality in C.

Remember to always free dynamically allocated memory in C to prevent memory leaks.