ExpressionEngine plugin that allows for a “contains” conditional as opposed to an “equals” or “doesn’t equal” conditional, by Erik Reagan
ExpressionEngine 1.6.x
You can use this plugin to only display something if the string is found OR you can
optionally supply data for if the string is not found
Returning TRUE only
{exp:if_contains needle="homepage" haystack="{page_body}"}
This shows up only if "homepage" is found in "{page_body}"
{/exp:if_contains}
Both TRUE and FALSE options
{exp:if_contains needle="homepage" haystack="{page_body}"}
This shows up if "homepage" IS found in "{page_body}"
{if_contains:else}
This shows up if "homepage" IS NOT found in "{page_body}"
{/exp:if_contains}
Multiple Needles
You can add multiple needles by piping your options together like this:
{exp:if_contains needle="homepage|about|contact" haystack="{uri}"}
This shows up only if either "homepage", "about", or "contact" is found in "{uri}"
{/exp:if_contains}
If your haystack contains single or double quotes this may cause an error. If you only have double quotes then you will want to wrap the haystack in single quotes and vise versa. For example
{exp:if_contains needle="Hi" haystack="John said, "Hi, how are you?""}
true....
{/exp:if_contains}
This will not work because the quotation of what John said actually cuts off the haystack variable. To get around this you could change it to this:
{exp:if_contains needle="Hi" haystack='John said, "Hi, how are you?"'}
true....
{/exp:if_contains}
1.1, July 8, 2010
- Feature: Added multiple needle options by piping (needle=“one|two|three|etc”)
1.0, March 19, 2010
- Initial Release