feat: add include functionality to support modular template composition

Implement the `include` directive that allows embedding external template files into the current template at render time. This enables code reuse and separation of concerns by letting templates import shared components like headers, footers, or partials. The feature parses include statements, resolves file paths relative to the template root, and recursively renders the included content within the parent template's variable scope.
This commit is contained in:
2025-11-23 14:23:59 +00:00
parent 77beada5a6
commit cfab51abd8
9 changed files with 394 additions and 15 deletions
+27
View File
@@ -0,0 +1,27 @@
#include "includes/math_lib.rc"
int main() {
printf("=== Include Directive Tests ===\n");
printf("Test 1: Basic include\n");
int sum = add(10, 5);
printf("add(10, 5) = %d\n", sum);
printf("PASS: Included function works\n");
printf("Test 2: Multiple functions from include\n");
int diff = subtract(20, 7);
printf("subtract(20, 7) = %d\n", diff);
int prod = multiply(6, 4);
printf("multiply(6, 4) = %d\n", prod);
int quot = divide(15, 3);
printf("divide(15, 3) = %d\n", quot);
printf("PASS: All included functions work\n");
printf("Test 3: Using included functions in expressions\n");
int result = add(multiply(3, 4), subtract(10, 5));
printf("add(multiply(3, 4), subtract(10, 5)) = %d\n", result);
printf("PASS: Included functions in expressions work\n");
printf("\n=== All Include Tests Completed ===\n");
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
if (b == 0) {
return 0;
}
return a / b;
}
+19
View File
@@ -0,0 +1,19 @@
int string_length(char *s) {
return strlen(s);
}
char* string_upper(char *s) {
return upper(s);
}
char* string_lower(char *s) {
return lower(s);
}
int string_contains(char *haystack, char *needle) {
int pos = strpos(haystack, needle);
if (pos >= 0) {
return 1;
}
return 0;
}
+14
View File
@@ -0,0 +1,14 @@
#include "math_lib.rc"
#include "string_lib.rc"
int is_even(int n) {
int half = divide(n, 2);
return multiply(half, 2) == n;
}
int is_odd(int n) {
if (is_even(n)) {
return 0;
}
return 1;
}
+22
View File
@@ -0,0 +1,22 @@
#include "includes/utils.rc"
int main() {
printf("=== Nested Include Tests ===\n");
printf("Test 1: Functions from nested includes\n");
int sum = add(5, 3);
printf("add(5, 3) = %d\n", sum);
printf("PASS: Functions from first-level include work\n");
printf("Test 2: Utility functions using nested includes\n");
if (is_even(10)) {
printf("is_even(10) = true\n");
}
if (is_odd(7)) {
printf("is_odd(7) = true\n");
}
printf("PASS: Utility functions using nested includes work\n");
printf("\n=== All Nested Include Tests Completed ===\n");
return 0;
}