kegs
kegs

Reputation: 21

Silverstripe Text Parser Function not returning value

Can someone please help troubleshoot my TextParser function which checks to see if the variable $SubTitle on my page template contains the word "Hire" and either display or hide a div block accordingly.

When $SubTitle does contain the word "Hire" the function is not returning visible. I assume it's something to do with $str = $SubTitle but not sure how to correctly write this??


class OrderTypeExtension extends TextParser
{
   function parse(){
   $str = $SubTitle;

   if (strpos($str, 'Hire') !== false)
   return 'visible';
   else return 'hidden';

   }
}

Upvotes: 0

Views: 80

Answers (1)

bummzack
bummzack

Reputation: 5875

If you're implementing the OrderItem subclasses yourself, you can just add a helper method to each one, with:

public function getVisibility()
{
    return strpos($this->SubTitle, 'Hire') === false ? 'hidden' : 'visible';
}

Or, if these OrderItem classes are coming from a module and should be left untouched, use an Extension.

class VisibilityExtension extends Extension
{
    public function getVisibility()
    {
        return strpos($this->owner->SubTitle, 'Hire') === false 
            ? 'hidden' : 'visible';
    }
}

Then add the VisibilityExtension to your OrderItem classes, as described in the docs.

After a dev/build you can use $Visibility in your template to either output hidden, or visible.

Upvotes: 1

Related Questions