We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin returns data in constructor but not a function

Development and Programming

johnwbaxter's avatar
johnwbaxter
651 posts
13 years ago
johnwbaxter's avatar johnwbaxter

I have a plugin constructed in the normal way that takes the placeholders inbetween it’s tag pair, and returns the data to the template using:

$this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $variables);

However, it only works if i have the login in the constructor but not a function.

Say i have

public function __construct()
 {
  // Get to the EE superglobal

  $this->EE =& get_instance();
                $variables[] = array('jeff','bob, 'barry'');
                $this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $variables);
        }

That works if you call {exp:plugin_name bob="stuff" barry="things"}{my_var1}{my_var2}{/exp:plugin_name} and it does indeed return the data.

But if i have the same code as the above but in it’s own function in the plugin class called

public function my_func()
{

}

and i do:

{exp:plugin:my_func name bob="stuff" barry="things"}{my_var1}{my_var2}{/exp:plugin_name:my_func}{my_var1}{my_var2}

it doesn’t output anything at all even though if i var_dump($this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $variables);) i can see that it has worked, the data in there is all that is within the tag pair and the data replacements have happened. It just doesn’t output it in the template!

Any ideas anyone?

Yes i still have the constructor in there and yes it still has $this->EE =& get_instance(); in it!

       
Aaron Waldon's avatar
Aaron Waldon
66 posts
13 years ago
Aaron Waldon's avatar Aaron Waldon

Hi johnwbaxter!

The difference is how the data is returned. If you are returning from the constructor, you use:

$this->return_data = $your_output_variable;

to hold the final content.

If you are returning from a class method, like my_func(), you actually return the output data:

return $your_output_variable;

Does that make sense?

Update: Here’s a link to the plugin documentation on Returning A Value.

       
johnwbaxter's avatar
johnwbaxter
651 posts
13 years ago
johnwbaxter's avatar johnwbaxter

Hi Aaron,

Ahhh totally forgot about that, just had a total mind blank as to why that wasn’t working, i was looking at a million other things as the cause other than that!

Thanks a lot!! 😊

John

       
Aaron Waldon's avatar
Aaron Waldon
66 posts
13 years ago
Aaron Waldon's avatar Aaron Waldon

My pleasure John. Glad I could help. 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.