Text Templates in COBOL

IDENTIFICATION DIVISION.
PROGRAM-ID. TEXT-TEMPLATES.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT STANDARD-OUTPUT ASSIGN TO DISPLAY
    ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD STANDARD-OUTPUT.
01 OUTPUT-LINE PIC X(80).

WORKING-STORAGE SECTION.
01 WS-TEMPLATE PIC X(50).
01 WS-VALUE PIC X(50).
01 WS-STRUCT.
   05 WS-NAME PIC X(20).
01 WS-ARRAY.
   05 WS-ARRAY-ITEM PIC X(10) OCCURS 4 TIMES.
01 WS-I PIC 9(2).

PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM SIMPLE-TEMPLATE
    PERFORM STRUCT-TEMPLATE
    PERFORM CONDITIONAL-TEMPLATE
    PERFORM ARRAY-TEMPLATE
    STOP RUN.

SIMPLE-TEMPLATE.
    MOVE "Value is {}" TO WS-TEMPLATE
    MOVE "some text" TO WS-VALUE
    PERFORM EXECUTE-TEMPLATE
    MOVE "5" TO WS-VALUE
    PERFORM EXECUTE-TEMPLATE.

STRUCT-TEMPLATE.
    MOVE "Name: {}" TO WS-TEMPLATE
    MOVE "Jane Doe" TO WS-NAME
    MOVE WS-NAME TO WS-VALUE
    PERFORM EXECUTE-TEMPLATE.

CONDITIONAL-TEMPLATE.
    MOVE "not empty" TO WS-VALUE
    IF WS-VALUE NOT = SPACES
       MOVE "yes" TO OUTPUT-LINE
    ELSE
       MOVE "no" TO OUTPUT-LINE
    END-IF
    WRITE OUTPUT-LINE
    MOVE SPACES TO WS-VALUE
    IF WS-VALUE NOT = SPACES
       MOVE "yes" TO OUTPUT-LINE
    ELSE
       MOVE "no" TO OUTPUT-LINE
    END-IF
    WRITE OUTPUT-LINE.

ARRAY-TEMPLATE.
    MOVE "Range: " TO OUTPUT-LINE
    MOVE "Go" TO WS-ARRAY-ITEM(1)
    MOVE "Rust" TO WS-ARRAY-ITEM(2)
    MOVE "C++" TO WS-ARRAY-ITEM(3)
    MOVE "C#" TO WS-ARRAY-ITEM(4)
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 4
       STRING OUTPUT-LINE DELIMITED BY SIZE
              WS-ARRAY-ITEM(WS-I) DELIMITED BY SIZE
              " " DELIMITED BY SIZE
              INTO OUTPUT-LINE
    END-PERFORM
    WRITE OUTPUT-LINE.

EXECUTE-TEMPLATE.
    MOVE FUNCTION SUBSTITUTE(WS-TEMPLATE "{}" WS-VALUE)
         TO OUTPUT-LINE
    WRITE OUTPUT-LINE.

This COBOL program demonstrates the concept of text templates, although COBOL doesn’t have built-in template functionality like Go. Here’s an explanation of the key parts:

  1. We define a simple template substitution using the SUBSTITUTE function, which replaces {} with the given value.

  2. The SIMPLE-TEMPLATE paragraph shows how to use this basic template with different types of values.

  3. The STRUCT-TEMPLATE paragraph demonstrates using a field from a structure (similar to struct in Go).

  4. The CONDITIONAL-TEMPLATE paragraph shows an if/else condition, which is similar to the conditional execution in Go templates.

  5. The ARRAY-TEMPLATE paragraph demonstrates iterating over an array, which is analogous to the range functionality in Go templates.

To run this COBOL program, you would typically compile it and then execute the resulting binary. The exact commands may vary depending on your COBOL compiler, but it might look something like this:

$ cobc -x text-templates.cob
$ ./text-templates

This would produce output similar to the Go program, demonstrating the basic concepts of text templates in COBOL.

Note that COBOL doesn’t have the same level of template functionality as Go, so this example is a simplified approximation of the concepts. In real-world COBOL applications, more complex string manipulation or report generation utilities might be used for similar purposes.