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

How do I create a conditional variable to use in a plugin I am creating?

Development and Programming

Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hi there,

I was just wondering how you go about creating a conditional variable that I can use within a plugin tag pair?

Let’s say I have the following plugin :

{exp:my_plugin_name my_plugin_variable="1"}
Content will show if plugin allows...
{/exp:my_plugin_name}

Basically let’s say for now that the plugin performs a SQL query on a certain field and if the field contains the value 1 as set via the my_plugin_variable=”1” then the content between the tag pair will show.

This all works fine with my current plugin and what I am actually doing is using the number of rows returned from the query and if it is more than a certain value then it allows the content between the tag pair to be output but what I would like to do now is have something like below :

{exp:my_plugin_name my_plugin_variable="1"}

{if not_allowed}
This content will show if no results...
{/if}

Content will show if plugin allows...
{/exp:my_plugin_name}

So basically what is happening here is if the query runs but the number of rows returned is above a certain number then I can set a variable called not_allowed to be TRUE and then use the {if not_allowed} code to show some other content instead of the content between the tag pair.

Hopefully I have explained this well enough and someone can help me figure this out.

Thanks to Mr Wilson I do have this code below :

global $FNS, $TMPL;

$maxed_out = TRUE;
$my_var = 6;
$another_var = 'Abraham Lincoln';

$my_vars = array(
  'maxed_out' => $maxed_out,
  'my_var' => $my_var,
  'another_var' => $another_var
  );

$output = $FNS->prep_conditionals($TMPL->tagdata, $my_vars);

return $output;

and I was pretty sure in my head that I was going to have to use the $FNS->prep_conditionals but I am just unsure of how to go about doing this.

Thanks in advance for any help with this.

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Can anyone perhaps throw me a life-line on this one. An admin perhaps. I’ll bet you can give me the answer in about 2 seconds flat! 😊

Thanks in advance for any help on this one.

Best wishes,

Mark

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Okay,

I have this sort of working but not fully. At the moment I have been able to get it to use the conditional variable and spit out whatever is between the

{if not_allowed}
Spit this text out
{/if}

but now if I spit out the

$this->return_data = $TMPL->tagdata;

then I get everything between the plugin tags. What I want to be able to do is either spit out what is between the {if} tags or everything else other than the code between the {if} conditional.

How would I go about doing that?

Thanks in advance for any help with this.

Best wishes,

Mark

       
Mr. Wilson's avatar
Mr. Wilson
131 posts
17 years ago
Mr. Wilson's avatar Mr. Wilson

Hi Mark,

You can do that in a couple different ways. One way doesn’t require conditionals at all. For example, consider this code:

{exp:my_plugin}
{fail}Sorry, bub, you don't have permission to be here.{/fail}
Hello, and welcome to MovieFone!
{/exp:my_plugin}

In your plugin, your steps would be something like:

1. Decide whether to show the content of the {fail /} var pair, or everything else
2. If fail, then
   a. Use a regex to fetch the contents of the {fail /} var pair
   b. Output just the contents of the var pair
3. If not fail, then
   a. Use a regex replace to get rid of the {fail /} var pair
   b. Output what's left

If you want to use conditionals, your template code might look like:

{exp:my_plugin}
{if fail}Sorry, bub, you don't have permission to be here.
{if:else}Hello, and welcome to MovieFone!{/if}
{/exp:my_plugin}

For that, we need $FNS->prep_conditionals(), which you might apply like this pseudo-code:

function my_plugin()
{
  global $FNS, $TMPL;

  $tagdata = $TMPL->tagdata;

  // We'll use this array to store variables that we'll allow
  // to be parsed as conditionals
  $conds = array();

  // Insert your plugin's voodoo here. Create some variables, check the DB, whatever

  ... blah blah blah ...

  // Determine if fail is TRUE or FALSE
  $conds['fail'] = (some condition) ? TRUE : FALSE;

  // Prep the output using EE's conditional voodoo
  $tagdata = $FNS->prep_conditionals($tagdata, $conds);

  // Spit it out
  $this->return_data = $tagdata;
}

Any help?

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Wow!! Thanks Mr Wilson.

I will look into the code you have shown as soon as I can. I had the understanding in my head as in I needed to take out the {if} part but just wasn’t too sure of how to go about it.

Will look at your code as soon as I can.

Once again thanks for all your help on this. Will keep you updated of my slow progress!! 😊

Best wishes,

Mark

       

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.