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

Outputting a Global Variable from a plugin?

Development and Programming

Stamp Idea Group's avatar
Stamp Idea Group
38 posts
14 years ago
Stamp Idea Group's avatar Stamp Idea Group

I’m developing a very small plugin for myself, and haven’t done this before, so I am sorry if this is a stupid question.

I am assigning a new global variable via the below code, and when using a print_r on the _global_vars array, I can see the variable assigned. However, when I try to output the variable inside of a template, nothing happens. Other variables will be replaced with their values, but the one I am assigning stays as just the variable name text. Is there some trick to making the variable output properly?

$this->EE->config->_global_vars = array_merge($this->EE->config->_global_vars, $site_data);

Where $site_data is my array key/variable pair.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

This is a parse order issue. What type of variable are you trying to set and why do you need it accessed from outside of your plugin? There is probably another way to achieve what you’re doing so let’s hear more about the problem you’re trying to solve 😊

       
Bhashkar Yadav's avatar
Bhashkar Yadav
727 posts
14 years ago
Bhashkar Yadav's avatar Bhashkar Yadav

you can use code like below in plugin method

return $EE->TMPL->swap_var_single('variable_name', $variable_value, $EE->TMPL->tagdata);

In the template {variable_name} will output the value of $variable_value.

hope this might help you.

       
Stamp Idea Group's avatar
Stamp Idea Group
38 posts
14 years ago
Stamp Idea Group's avatar Stamp Idea Group

Basically I am using this as a learning exercise, it isn’t for a real project. I am trying to set a variable in the plugin and have it accessible in the template for testing purposes.

{if my_variable == 'value'}
    execute logic
{if:else}
    other logic
{/if}

It is nothing complicated, I can accomplish what I want by enabling php in the template, but thought this would be a “safer” way to handle accessing a dynamic variable. A plugin may not be the best way to handle this, but I thought this would an extremely simple thing to accomplish and a plugin was the easiest addon to create. Perhaps another addon type would be better, I’m not sure. Any suggestions would be appreciated, I’ve been reading through the docs the last few days when I have spare time.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Sounds like you’d benefit from checking out Stash which done just that. It’s a free add-on so it would likely be a nice reference for your learning. The author (Mark Croxton) writes really good code.

http://devot-ee.com/add-ons/stash

       
creding's avatar
creding
2 posts
14 years ago
creding's avatar creding

I would create a function in the plugin named for the variable then return the value which would appear as if it was a global var like this

{exp:my_plugin:my_var}

Replace my_plugin with your plugin name and my_var with the function name

Inside the plugin add the function

function my_var() { $val = “whateva”; return $val; }

Should work in theory

       
Stamp Idea Group's avatar
Stamp Idea Group
38 posts
14 years ago
Stamp Idea Group's avatar Stamp Idea Group

That method would work for outputting a value / “variable” in theory, but would not work for doing logic tests. I’ve temporarily forgotten about this since I’m too busy with other projects, but I appreciate everyone’s feedback.

       

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.