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

Passing variables to Plugins as Parameters

Development and Programming

hereward's avatar
hereward
91 posts
17 years ago
hereward's avatar hereward

I have been developing a few plugins and I have found the API to be useful and convenient, however thre is a problem.

When i try to pass a variable through the parameter list is comes up null in PHP.

Template example (intro is a custom field in the weblog “my_weblog”):

{exp:weblog:entries weblog="my_weblog"}
   {exp:my_plugin:my_method my_variable="{intro}"}
{/exp:weblog:entries}

PHP usage:

$my_variable = $TMPL->fetch_param('my_variable');

In the above usage the variable

$my_variable

would be empty.

Moved from Tech Support by Moderator

       
hereward's avatar
hereward
91 posts
17 years ago
hereward's avatar hereward

Seems like nobody is interested in this topic? If the question is too stupid please let me know.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Hi hereward,

Are you positive that {intro} is being parsed to what you expect?

Try something like:

{exp:weblog:entries weblog="my_weblog"}
   Variable: {intro}
   {exp:my_plugin:my_method my_variable="{intro}"}
{/exp:weblog:entries}

And have the plugin return (or better yet var_dump()) the variable.

What do you see?

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

You need to put ‘parse=”inward”’ in the entries plugin to get it to process first.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Actually the entries tag is a weblog module tag, and does not need that parameter to be parsed before plugin tags contained within the entries tag, Erin. That parameter is used when you have multiple nested plugin tags that you need to outer to be parsed first. the_butcher’s advice is correct in this case, hereward.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

Whoops, I knew that..I just solved a parse problem myself with the ‘inwards’ parameter and forgot that.

Sorry for the wrong information.

       
hereward's avatar
hereward
91 posts
17 years ago
hereward's avatar hereward
Hi hereward, Are you positive that {intro} is being parsed to what you expect? Try something like:
{exp:weblog:entries weblog="my_weblog"}
   Variable: {intro}
   {exp:my_plugin:my_method my_variable="{intro}"}
{/exp:weblog:entries}
And have the plugin return (or better yet var_dump()) the variable. What do you see?

Hi thanks for your reply. i have been away for a while. I am not sure what you are driving at there, but I can assure you that the variable has valid data in it.

In fact I was able to get the plugin working using the variable as parsed data rather than as a plugin parameter, so I think I have already addressed this question.

example:

{exp:weblog:entries weblog="my_weblog"}
   {exp:my_plugin:my_method}
      {intro}
   {/exp:my_plugin}
     
{/exp:weblog:entries}

The problem is, when the variable is passed as a parameter to the plugin it becomes null when the PHP executes the code.

As a final test … does anybody have a working example of ANY custom plugin that can receive variables as parameters? If anyone can demonstrate that such a beast exists i would eat my hat.

Thanks (:

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

Hmm, too many to mention. Including most of the bundled 1st party plugins. You have referenced the global $TMPL object, yes?

<?php
class my_plugin
{
  function my_plugin()
  {
  }
  function my_method()
  {
    global $TMPL;
    var_dump($TMPL->fetch_param('my_variable'));
  }
}
?>
       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

Yes, the plugin itself has no bearing on whether or not another tag’s variable can be used as a parameter. I have a feeling that your parameter is being broken by either a quote in the data or in field formatting for {intro} (auto XHTML, Markdown, etc.).

       

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.