Making a variable available both inside and outside conditionals
Posted: 16 June 2008 07:49 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 16 June 2008 08:28 AM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

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

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 16 June 2008 11:34 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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?

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 16 June 2008 11:40 AM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

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!! wink

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

Best wishes,

Mark

 Signature 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 16 June 2008 11:03 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 17 June 2008 01:03 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  246
Joined  04-12-2008

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

.

 Signature 

Designchuchi | Twitter

URL Field Extension
Required Category Extension
DC FreeForm GeoIP Extension

Profile
 
 
Posted: 17 June 2008 07:48 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64909 Total Logged-in Users: 44
Total Topics: 81853 Total Anonymous Users: 29
Total Replies: 440065 Total Guests: 239
Total Posts: 521918    
Members ( View Memberlist )