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"}
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"}
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}
Compatibility with Articles Anywhere
If you plan to also use Articles Anywhere If Structures in your Snippet, for example, to do checks on custom fields, you may run into incompatibility issues.
This is because both Articles Anywhere and Snippets share the same If Structures syntax by default.
In this case, to make sure that If Structures from both extensions can be used at the same time, you should change the default Tag Character in the Settings.