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
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
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?
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
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.