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

Making a variable available both inside and outside conditionals

Development and Programming

Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi,

I know how to output some data through a variable. This is possible using the code as this:

$tagdata = str_replace('{my_variable}', $my_variable, $tagdata);
$this->return_data = $tagdata;

I know also how to make a variable to be available in conditionals. This is possible using the code as this:

$conds['my_variable'] = $my_variable;
$this->return_data = $FNS->prep_conditionals($TMPL->tagdata, $conds);

But how to achieve both? The lines

$this->return_data = $tagdata;

and

$this->return_data = $FNS->prep_conditionals($TMPL->tagdata, $conds);

if put one after the other cancel the one which is put the first.

So, anyone who knows intricacies of plugin writing, please be so kind, explain how to make a variable available both inside and outside conditionals.

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

Hiya,

I’m not the best at that kind of thing but would this work?

$this->return_data = $tagdata;

$this->return_data .= $FNS->prep_conditionals($TMPL->tagdata, $conds);

Note the dot (.) before the second = sign.

Don’t know if that helps at all?

Best wishes,

Mark

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Mark,

Thanks for suggestion. Unfortunately it does not offer proper solution: tagdata gets outputted twice - once with variable rendered correctly, but with conditional not rendered, and once with variable not rendered, but with conditional rendered correctly.

Any new suggestions?

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

Well as I said before I’m not really the best person at this sort of thing as I am still learning it all myself but perhaps you could show the code that you are using at the moment in your plugin and then I can take a look and also many others on the forums too. I’m sure that if I don’t get it that someone else will. In fact I’m also sure that someone else will have the answer probably about 1,000 times quicker than me too!! 😉

Some code that you are trying to do would help though in any case.

Best wishes,

Mark

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Mark,

My code is very simple. I want to have the variable {entries_number} to be available for use both inside and outside conditionals. Now I succeed to achieve only one thing but not both.

The code is here:

//Create conditionals array
    $conds['entries_number'] = $entriesnumber;
    
    // Check if there is {entries_number} variable placed between {exp:entries_number} and {/exp:entries_number} tag pair
    if (strpos($tagdata, '{entries_number}') > 0 OR strpos($tagdata, '{entries_number}') === 0)
    {
      // If there is {entries_number} variable, then return entries number as variable's output
      $tagdata = str_replace('{entries_number}', $entriesnumber, $tagdata);
      $this->return_data = $tagdata;
    }
    else
    {
      // If there is no {entries_number} variable, then make entries_number variable available for use in conditionals
      $this->return_data = $FNS->prep_conditionals($TMPL->tagdata, $conds);
    }

Any suggestion appreciated.

       
gridonic's avatar
gridonic
231 posts
17 years ago
gridonic's avatar gridonic

Have a look at some of the core files (preferably the smaller modules), they’re doing in there exactly what you’re trying to achieve, also you might need to use

$TMPL->swap_var_single

.

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Peter,

Thanks for suggestion. Studying the files I found out that instead of writing

$this->return_data = $FNS->prep_conditionals($TMPL->tagdata, $conds);

it is possible to write

$tagdata = $FNS->prep_conditionals($tagdata, $conds);

This mode of introducing conditionals does not mention return_data and is perfectly compatible with the statement

$this->return_data = $tagdata;

So, now I can both to output some data through a variable and make the variable to be available in conditionals by writing the code as this:

$conds['my_variable'] = $my_variable;
$tagdata = $FNS->prep_conditionals($tagdata, $conds);
$tagdata = str_replace('{my_variable}', $my_variable, $tagdata);
$this->return_data = $tagdata;

Concerning the function swap_var_single I am not sure, whent it should be used. str_replace function does the job and it seems that in EE core files str_replace function is used quite often in places where you might expect to find swap_var_single.

Thanks again, Mark and Peter, for your 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.