If Conditionals
Learn the fundamentals on how to write if statements in Parsley.
Parsley provides {{if}}
conditional statements for logic flow.
Checking Equality
When writing if conditionals you can check equality with a single equals character; ==
. Equality can also be inverted with a bang; !=
.
If you're evaluating another Parsley expression including autogenerated fields inside of an if conditional use single curly brackets to wrap your expression. If you are expecting this variable to be a string (this includes ZUIDs) be sure to wrap it in quotes.
(** Inner Parsley expression **)
{{ if {this.title} == 'Hello, World' }}
It works!
{{end-if}}
(** Inverted equality **)
{{if {item.zuid} != {this.item_page} }}
<a href="/" class="home"><img src="..." /></a>
{{end-if}}
(** checking if a value exists on a content item **)
{{if {this.image} }}
<img src="{{this.image.getImage()}}" alt="{{this.image_text}}">
{{end-if}}
If / Else
When writing an if conditional you can also specify an else condition.
{{ if {page.path_part} == 'special-page' }}
This is the special page!
{{else}}
This is not the special page!
{{end-if}}
If / Else If / Else
You may have multiple conditionals using else if
{{ if {page.path_part} == 'special-page' }}
This is the special page!
{{else if {page.path_path} == 'another-path' }}
This is another page {{include another-snippet}}
{{else}}
This is neither pages :(
{{end-if}}
Updated 9 months ago