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