When using Dynamic Snippets, you can also use (simple) if-else structures to conditionally output content based on the passed variables.
The if structures tag syntax looks like this:
{if ...}...{elseif ...}...{else}...{/if}
Check if variable passes
The if-check will pass if the variable you are checking against is passed to the snippet and is not empty, false
or 0
.
For example, to check whether a variable year
was passed to the Snippet:
{if year}...{/if}
This allows you to output a piece of text, only if that variable exists:
The company %company% is based in %country%{if year} and was founded in %year%{/if}.
{snippet alias="my-snippet" company="Regular Labs" country="The Netherlands" year="2008"}
The company Regular Labs is based in The Netherlands and was founded in 2008.
Alternative content if variable doesn't pass
If you want to show a different text when the Snippet doesn't have a year
variable, you can do:
{if year} and was founded in %year%{else} but we don't know when it was founded{/if}.
{snippet title="Company" company="Irregular Wrecking Yards" country="Belgium"}
The company Irregular Wrecking Yards is based in Belgium but we don't know when it was founded.
To only place-specific content if the variable doesn't exist (meaning it's empty or was not passed), you can also use:
{if !year}...{/if}