Documentation

Case statements (switch)

Similar to the if / else the case or "switch" statement executes some code if a condition is true.

Example:

<%= case when 1 = 1 then 'This will render.' end %>

You can provide multiple "cases" and it will check each condition until one resolves to true. At this point it will execute the relevant code block and not process the remainder of the statement.

Example:

<%= case when 1 = 2 then 'This will not execute.' when 1 = 1 then 'I will execute!' end %>

Finishing a Case with an else fallback executes some code if none of the conditions resolve to true.

Example:

<%= case when 1 = 2 then 'This will not render' when 1 = 3 then 'I also will not execute :(' else 'I will!!!' end %>

For more information on conditionals and other control structures, check PostgreSQL documentation.