Conditional text

Applies to: All programs with data


Text fields in templates can allow for conditional processing of text based on whether a certain condition is true or false. This is perhaps best explained by way of example

Consider a sports photographer offering trader cards with the following fields; name, position and height. The template might look something like this:

  • Name: @Name
  • Position: @Position
  • Height: @Height
When all fields contain data the trader card will correctly display
  • Name: Sam Jackson
  • Position: Midfield
  • Height: 5' 2"

Now consider a situation where the player has not supplied his or her position and subsequently the position field is empty. This will result in the following

  • Name: Sam Jackson
  • Position:
  • Height: 5' 2"

It would be preferable if the header "Position" did not appear under these circumstances. Conditional text support will determine if the Position field contains data and process the Position: @Position text string accordingly. This is achieved by modifying the text string as follows:

  • Name: Sam Jackson
  • {@Position|Position: @Position}
  • Height: @Height

Upon encountering a { character, everything up to the upright bar | becomes a condition. If text results from the condition(i.e. the condition is true), everything after the upright bar |is used. If no text results from the condition (i.e. the condition is false) the text is ignored. This would result in the following output.

  • Name: Sam Jackson
  • .
  • Height: 5' 2"

The blank line is also undesirable but we can incorporate the carriage return into the condition as follows:

  • Name: @Name
  • {@Position|Position: @Position
  • }Height: @Height

This will result in the following

  • Name: Sam Jackson
  • Height: 5' 2"

Since there is nothing in the Position field and the condition is therefore false everything between the curly brackets is ignored including the carriage return (enter).
Of course the conditional processing can be extended to all the fields as height data may also not be available. For example.

  • Name: @Name
  • {@Position|Position: @Position
  • }{@Height|Height: @Height
  • }

Furthermore it is possible to optionally specify alternative text to use when the condition is false by simply adding another upright bar and the replacement text. In the example above we may prefer to have the word "Utility" appear if no Position data is available. In this case we modify our text string as follows.

  • Name: @Name
  • {@Position|Position: @Position|Utility}
  • {@Height|Height: @Height}

Now if the @Position is false i.e. there is no data in the Position field the word Utility will be used. This results in:

  • Name: Sam Jackson
  • Position: Utility
  • Height: 5' 2"

Due to the use of the curly brackets, { } in this way if you need to use the curly brackets in your text string simply enter double {{

End of article