I can use a string template like so:
st = StringTemplate["a is `a` and b is `b`"];
st @ <|"a" -> 1, "b" -> 2|>
which gives
"a is 1 and b is 2"
But I would like the parameter b
to be optional, so that if I call
st @ <|"a" -> 1|>
I get simply
"a is 1"
Something like this is possible with <* *>
: For the case where b
must be numeric, I can use
StringTemplate["a is `a`<* If[NumericQ[#b],\" and b is \",\"\"]*>`b`"]
but is there a way to tell whether a template parameter is present or absent, rather than numeric or otherwise?
KeyMemberQ[#,"b"]
to check whether"b"
is present in the parameters. But I'm not sureStringTemplate
is really the best option for something like this... $\endgroup$