How to dynamically build a Cheetah3 placeholder variable name?

83 views Asked by At

The bash shell lets you do this with ${!variablename}. This will retrieve the bash variable whose name is contained within the variable named 'variablename'. I'm looking for a way to do that same thing with Cheetah3 (not with necessarily the same syntax, of course - but I am looking for some way to achieve that capability within Cheetah3 itself.)

Here's an example using bash to show what I mean:

% ONE="1"
% TWO="2"
% CHOICE="ONE"
% echo ${!CHOICE}
1
% CHOICE="TWO"
% echo ${!CHOICE}
2
1

There are 1 answers

0
phd On

I'm not sure it's possible. My advice is to use a dictionary:

$variables = {
    "ONE": "1",
    "TWO": "2",
}

$CHOICE = "ONE"
echo $variables[$CHOICE] # -> 1

$CHOICE = "TWO"
echo $variables[$CHOICE] # -> 2