The [if][else][/if] conditional block gives added functionality to your Pods Templates by giving you the ability to use simple “if this exists, display this, else do this instead” logic.
The previous example illustrating an [each] for a relationship field has a major problem: if there are no chapters set it would always output empty.
An empty [each] could result in the output below.
<h3>Chapters</h3> <ul> </ul>
Contents
IF Check against Related or LOOP Content
To avoid, this the each loop can be placed inside of if conditionals, so that nothing is outputted if there are no chapters to output:
[if chapters]
<h3>Chapters</h3>
<ul>
[each chapters]
<li>{@post_title}</li>
[/each]
</ul>
[/if]
You could use this same methodology before looping through connected Taxonomy, File/Image Fields or Relationships
IF using ELSE Example
Now there will be no output if there are no related chapters to return. You can even take this a step further by adding alternate output, if the conditional is not met, using an else statement. For example:
[if chapters]
<h3>Chapters</h3>
<ul>
[each chapters]
<li>{@post_title}</li>
[/each]
</ul>
[else]
<p>Sorry, No chapters</p>
[/if]
IF Check Against a Field
If conditionals are also useful when you wish to show a label before a field, but do not want that label to appear if the field has no value. In the following example, the text “Co Author:” only appears if there is a value in the ‘co_author’ field.
[if co_author]
Co Author: {@co_author}
[/if]
ELSE tag limitations
The [else] tag does not support any conditionals at all, so it will not work like a PHP elseif could. We may eventually add support for an [elseif] tag that uses the same syntax as [if] in the future.
New in Pods 2.8: Comparisons
There are now new comparisons available for the [if] tags in Pods 2.8+.
The [if] shortcode now supports the following attributes:
field="my_field_name"(optional, you can use[if my_field_name]for shorthand syntax to check if not empty)value="5"(optional, default is checking if not empty)compare="<"(optional, default is “=”)- NOTE: These attributes must be given in this order for now.
The compare attribute supports the following options for comparisons:
=(default if attribute value is set; current field value == shortcode attribute value)!=(current field value != shortcode attribute value)<(current field value < shortcode attribute value)<=(current field value <= shortcode attribute value)>(current field value > shortcode attribute value)>=(current field value >= shortcode attribute value)LIKE(use % for wildcards, NO regex support)NOT LIKE(use % for wildcards, NO regex support)IN(comma-separated value)NOT IN(comma-separated value)EXISTS(if the value is not null AND is not an empty array)NOT EXISTS(if the value is a null OR is an empty array)EMPTY(default if no value is provided; if the value is empty, based on the definition of empty by the field type)NOT EMPTY(if the value is NOT empty, based on the definition of empty by the field type)
Here are a few examples that are now possible:
<!-- Check if the current field value is empty --> [if my_field_name] [if field="my_field_name"] [if field="my_field_name" compare="EMPTY"] <!-- Check if the current field value is not empty --> [if field="my_field_name" compare="NOT EMPTY"] <!-- Check if the current field value matches exactly --> [if field="my_field_name" value="5"] [if field="my_field_name" value="5" compare="="] [if my_field_name="5"] <!-- Check if the current field value does not match exactly --> [if field="my_field_name" value="5" compare="!="] <!-- Check if the current field value is less than the "value" specified --> [if field="my_field_name" value="5" compare="<"] <!-- Check that the current field value is less than or equal to the "value" specified --> [if field="my_field_name" value="5" compare="<="] <!-- Check that the current field value is greater than the "value" specified --> [if field="my_field_name" value="5" compare=">"] <!-- Check that the current field value is greater than or equal the "value" specified --> [if field="my_field_name" value="5" compare=">="] <!-- Check if the current field value contains "some value" --> [if field="my_field_name" value="some value" compare="LIKE"] <!-- Check if the current field value begins with "some value" --> [if field="my_field_name" value="some value%" compare="LIKE"] <!-- Check if the current field value does not contain "some value" --> [if field="my_field_name" value="some value" compare="NOT LIKE"] <!-- Check if the current field value does NOT begin with "some value" --> [if field="my_field_name" value="some value%" compare="NOT LIKE"] <!-- Check that the current field value is either "this" or "that" --> [if field="my_field_name" value="this,that" compare="IN"] <!-- Check that the current field value is NOT "this" or "that" --> [if field="my_field_name" value="this,that" compare="NOT IN"] <!-- Check that the current field value is set at all, even if empty --> [if field="my_field_name" compare="EXISTS"] <!-- Check that the current field value is NOT set at all, even if empty --> [if field="my_field_name" compare="NOT EXISTS"]
New in Pods 2.8: Special field notations
There are now special field notations available for you to reference. These exist on every pod/field for you to reference. You can see a complete list of what is supported on the Pods::field() reference page.
Here are some of the most useful examples:
<!-- Check that the current pod matches a specific pod name --> [if _pod="my_pod"] [if field="_pod" value="my_pod"] [if field="_pod" value="my_pod" compare="="] <!-- Check that the current pod does NOT match a specific pod name --> [if field="_pod" value="my_pod" compare="!="] <!-- Check that we are on the first item in a list --> [if _position="1"] [if field="_position" value="1"] [if field="_position" value="1" compare="="] <!-- Check that we are on the third item in a list --> [if field="_position" value="3"] [if field="_position" value="3" compare="="] <!-- Check that there are more than 5 items in the list --> [if field="_total" value="5" compare=">="] <!-- Check that there are more than 2 pages of items to list (when using pagination) --> [if field="_total_pages" value="2" compare=">="] <!-- Check that there are more than 30 items found in total (when using pagination, otherwise it just matches what "_total" is) --> [if field="_total_found" value="30" compare=">="]