1

Is it possible to create an {if} statement based on the character length of a channel field? Something along the lines of this:

Lets say I have the channel field - {title} and I want to add a class to the parent <div> if the character length of the title is larger than 10. Something along these lines...

{if "{title_length}" >= "10"}
    <div class="wide">{title}</div>
{if:else}
    <div>{title}</div>
{/if}

How would I go about doing this with EE?

I hope this is clear. Thanks in advance.

Ryan

0

You can do this with PHPStringFun like so:

{if {exp:phpstringfun function="strlen"}{title}{/exp:phpstringfun} >= "10"}
    <div class="wide">{title}</div>
{if:else}
    <div>{title}</div>
{/if}

If you aren't getting the results you expect check what value is being returned by the plugin by just outputting it in your template like {exp:phpstringfun function="strlen"}{title}{/exp:phpstringfun}

CE String is an excellent plugin as well which you could use instead of PHPStringFun. Worth taking a look at. With CE String you'd do:

{if {exp:ce_str:ing strlen}{title}{/exp:ce_str:ing} >= "10"}
    {title}
{if:else}
    {title}
{/if}
| improve this answer | |
0

There are several plugins with which you can determine a string length. Here is one: https://devot-ee.com/add-ons/stringlength

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.