0

i'm trying to write my own plugin and new to php to simplify i want to grab a string from the tempalte then do something with it How do i call getMyString() in another function?

EDIT:

class test
{

    public $return_data = "";

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

    public function getMyString() {
        $tagdata = $this->EE->TMPL->tagdata;
        return "<p>TEST : " . $tagdata . "</p>";
    }

    public function newFuction() {
          $this->return_data = self::getMyString();
    } 
    // usage stuff here

}

Thanks!

1 Answer 1

1
$this->return_data = $this->getMyString();

return_data should only be used for the construct method (nod to Derek ;).

In the EE template:

{exp:test}woo hoo{/exp:test}

or

{exp:test:getMyString}woo hoo{/exp:test:getMyString}

I appreciate this is only a test, but you can't return anything from the construct if you're returning stuff from the methods - because the construct will always get called, therefore always return something, then the method will return something - so you'll get incorrect output.

To debug, simply put:

echo "<pre>";var_dump($tagdata);echo "</pre>";

...before the return in the getMyString method.

Your comment mentioned this:

{exp:test} {exp:test:getMyString}My Pretty string here...{/exp:test:getMyString} 

The first {exp:test} tag shouldn't be there (just checking ;) as that will only output "TEST: " because there is no tagdata and as mentioned, it may mess with the tag pair {exp:test:getMyString} because it outputs something.

3
  • Small correction - $this->return_data should only be used by your plugin's constructor function (since it can't return a value) - so with {exp:my_plugin}. When calling a function within your plugin class ({exp:my_plugin:my_function}), always return directly from your function. Sep 19, 2013 at 12:43
  • yes but for some reason even with $this->return_data = $this->getMyString(); in the construct it is not returning the value between {exp:test} {exp:test:getMyString}My Pretty string here...{/exp:test:getMyString} the {exp:test} only treturns <p>TEST : </p> from the above code but not the string itself
    – Visual
    Sep 20, 2013 at 6:53
  • Can you update your answer to include the EE code please. I'll update my answer to reflect your new code. Sep 20, 2013 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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