aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Linskey2026-06-07 02:15:36 -0400
committerBenjamin Linskey2026-06-07 02:15:36 -0400
commitc23b240bc382af4f97eb9cb9bada8b32180c341b (patch)
tree915d626fe5fb599d38ee0a4d56ef1fa60f74cb9d
parent0cd3a0bacd54eecf2a1339dd99120fc0c3280a88 (diff)
downloadtpl-c23b240bc382af4f97eb9cb9bada8b32180c341b.tar.gz

Allow vars to reference vars defined earlier

The template variable syntax can be used when defining a variable to reference the value of a previously defined variable:

foo = bar
baz = {{ foo }}
-rwxr-xr-xtemplate12
1 files changed, 11 insertions, 1 deletions
diff --git a/template b/template
index a125255..ef1a72e 100755
--- a/template
+++ b/template
@@ -3,7 +3,17 @@
BEGIN { FS = "[[:space:]]*=[[:space:]]*" }
FILENAME == ARGV[1] && /^#/ { next }
-FILENAME == ARGV[1] && NF == 2 { vals[$1] = $2 }
+FILENAME == ARGV[1] && NF == 2 {
+ # If the value is a template variable that matches a previously defined
+ # variable, use that variable's value. Otherwise, use the literal
+ # value.
+ if (match($2, /\{\{[[:space:]]*[^\{\}[:space:]]+[[:space:]]\}\}/)) {
+ match($2, /[^\{\}[:space:]]+/)
+ vals[$1] = vals[substr($2, RSTART, RLENGTH)]
+ } else {
+ vals[$1] = $2
+ }
+}
FILENAME == ARGV[1] { next }
/\{\{[[:space:]]*[^\{\}[:space:]]+[[:space:]]*\}\}/ {