I have seen tokens like this:
var message = "Hello, {Name}";
and like this:
var message = "Hello, ${name}";
and like this:
var message = "Hello, @NAME";
and a few other styles over the years.
Is there a common or preferred token format, style or character used in creating a set of tokens?
I have tried to used constants, like so,
"The width is WIDTH"
but I ran into problems when I had a token phrase that was contained in another token phrase.
For example, I had WIDTH and PERCENT_WIDTH as tokens. If the string contained both tokens one token could be replaced by the other in the first run through.
So if the token replacement value was 50 and the string was:
"The width of the item is WIDTH. That is PERCENT_WIDTH of the total."
then I ended up with,
"The width of the item is 50. That is PERCENT_50 of the total."
It wouldn't find "PERCENT_WIDTH" because that token string was modified.
So if I replaced the "PERCENT_WIDTH" token first that would solve that problem but as I added more tokens that would increase the chances for errors. So now I'm trying to figure out a recommended token format.
I'm using regex to find and replace matches.
There appears to be no "tokens" topic tag.
UPDATE
For those reading later on. Adding a symbol in front of a reserved token name prevents the problems I mentioned above.
So for example,
"WIDTH, PERCENT_WIDTH" ends up as "50, PERCENT_50" if you replace WIDTH first.
But if you prepend a character or symbol (glyph whatever) to the token replacement order doesn't matter.
"$WIDTH, $PERCENT_WIDTH" ends up as "50, 50%" no matter what order you replace the tokens
UPDATE 2
I realized last night there might be a case where using the symbol and the phrase like this, $WIDTH
, would break but I can't remember it now. I just remember thinking that I need to use brackets to enclose the string. Wait I think I'm remembering it.
So if you have $WIDTH and $WIDTH_PERCENT then you'd run into the same problem above.
So if the token replacement value was 50 and the string was:
"The width of the item is $WIDTH. That is $WIDTH_PERCENT of the total."
then you'd end up with,
"The width of the item is 50. That is 50_PERCENT of the total."
So now, I'm going to try to use {WIDTH} and see if that causes any issues.
"The width of the item is {WIDTH}. That is {PERCENT_WIDTH} of the total."
or whatever.{WIDTH}
won't match{PERCENT_WIDTH}
. This can work well if you know that these special characters won't appear in the data itself; if they do (if you need to support escaping them), it becomes more complicated.