From this question we know how to use a ternary operator to output conditional text: Is there a Twig shorthand syntax for outputting conditional text
Example:
{{ foo ? 'yes' : 'no' }}
How can we use a ternary operator to conditionally set a variable, without outputting it directly?