0

I want to display one field, called {gearboxx_body}, unless that field has more then 300 characters, in which case I want to display a field called {article_blurb}. I'm pretty sure there isn't a way to do this just in EE fields and conditionals, so I tried some PHP, which I'm just starting to learn:

<?php 

  $info = array('{gearboxx_body}','{article_blurb}');

  if(mb_strlen($info[0]) <= 300) 
    echo($info[0]);
  }

  else {
   echo($info[1]);
  }

?>

So that works well, but there's a problem. If the tag includes any apostrophes or quote marks, it ends the string and the page won't load. So what can I do about this? I've tried to replace the quote marks in the string, but I have to have loaded the string from the fields first, and as soon as I do that the page is already broken.

Hopefully that made sense. Any suggestions?

EDIT: I should also mention that I do not want to install an external plugin.

1
1

You say you don't want to do this with a plugin but the fact is that it's the best way to do it. Not least because you can avoid the problem you have with Parse order.

The plugin needs a single line of PHP In the constructor, and returns a number;

public function __construct() {
    $this->EE =& get_instance();

    return strlen($this->EE->TMPL->tagdata);
}

That's it.

You can quickly get the framework for your own plugin from http://pkg.io

Then you can use it like so

{if "{exp:stringlength}{my-custom-field}{/exp:stringlength}" > 200}
    More than 200 characters
{if:else}
    Less than 200 characters
{/if}

Because you are effectively outputting the field contents to your template and then getting its length you avoid the problem of characters prematurely terminating the comparison.

** Update ** In a quiet moment I wrote a quick and dirty plugin to provide a wrapper around PHP's strlen() function which might prove useful in this case. http://devot-ee.com/add-ons/stringlength

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.