Help needed please? - Can’t get my head around swap_var_single…
Posted: 06 July 2008 10:41 AM   [ Ignore ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

Hi there,

Just wondering if someone could help me with something that I thought I was doing right but I’m obviously not as it just isn’t working wink

What I want to do is this :

pi.test.php

{exp:test}
{my_variables}
{
/exp:test}

Please for now don’t shout at me for the bad naming convention of the plugin, this really is just a test at the moment wink

Now I have this code in the pi.test.php plugin :

<?php
/*
=========================================================================
Copyright (c) 2008 Mark Bowen Design
=========================================================================
File: pi.test.php V1.0.0
-------------------------------------------------------------------------
Purpose: Test
=========================================================================
CHANGE LOG :

6th July 2008
    - Version 1.0.0
    - Creation of initial plugin
=========================================================================
*/


$plugin_info = array(
    
'pi_name'            => 'Test',
    
'pi_version'        => '1.0.0',
    
'pi_author'            => 'Mark Bowen',
    
'pi_author_url'        => 'http://www.markbowendesign.com/',
    
'pi_description'    => 'Test',
    
'pi_usage'            => Test::usage()
);


class
Test {
    
var $return_data = '';

    function
test()
    
{
        
        
global $TMPL, $DB, $SESS, $LOC, $FNS;    
        
$tagdata = $TMPL->tagdata;
//        echo $tagdata;

        
$numbers = "12";
        
$tagdata = $TMPL->swap_var_single($my_variables, $numbers, $tagdata);
        
$this->return_data = $tagdata;

    
}





// ----------------------------------------
//  Plugin Usage
// ----------------------------------------
function usage()
{
ob_start
();
?>


EXAMPLE CODE
---------------------------------
{exp:test}
{my_variables}
{
/exp:test}



<?php
$buffer
= ob_get_contents();
    
ob_end_clean();

return
$buffer;
}
// END
}
?>

Basically as you can see in the plugin I am setting $numbers=“12”. I then wanted to be able to spit that out in place of the {my_variables} variable that exists in between the tag pair.

I have read and re-read over the documentation for this but truly it is just going over my head at the moment and I thought that I was doing this correctly. If I do this though :

$tagdata = "12";
$this->return_data = $tagdata;

it does return what I am after. How though do I make it do that with the variable?

Any help would be greatly appreciated. I know I must be missing something obvious here but just can’t see the forest for the trees at the moment! wink

Best wishes,

Mark

 Signature 

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

Profile
 
 
Posted: 06 July 2008 10:45 AM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

Aggggggggggghhhh!!!

Admins please feel free to delete this post and the one above!!

I must be totally thick or something!

Tried this instead :

$tagdata = $TMPL->swap_var_single('my_variables', $numbers, $tagdata);

Now all working!! Really weird as I took the code from another plugin which did it in the same way as my first post where they had $my_variables instead of ‘my_variables’ but that wouldn’t work!

Hopefully what I am doing here is correct now though? All seems to be working but just want to check before I carry on aimlessly again!! wink

Thanks.

Best wishes,

Mark

 Signature 

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

Profile
 
 
Posted: 06 July 2008 01:53 PM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  07-02-2007

Sir,

This is much faster en “easier”: (and does the same thing)

<?php

$tagdata
= str_replace('{my_variable}', $numbers, $tagdata);

?>


Btw you don’t need to make a separate variable out of your tagdata (unneeded overhead)

<?php
$TMPL
->tagdata = str_replace('{my_variable}', $numbers, $TMPL->tagdata);
?>

Since $TMPL->swap_var_single does this:

/** ---------------------------------------
    /**  Swap single variables with final value
    /** ---------------------------------------*/

    
function swap_var_single($search, $replace, $source)
    
{
        
return str_replace(LD.$search.RD, $replace, $source);  
    
}

You see? It is also doing ‘str_replace’, so why not do it yourself directly smile


P.S. str_replace() accepts arrays too smile, i use it to replace a bunch of single variables at once.

 Signature 

Truly ExpressionEngine


Fielder Module ( Mass Custom Fields )
Super Cache (Cache heavy tag output)
reCAPTCHA Extension
Rewrite Module


See all my EE Addons (8)

Profile
 
 
Posted: 06 July 2008 03:27 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  582
Joined  04-29-2008

Greetings,

Using:

$TMPL->swap_var_single('my_variable', $replace, $TMPL->tagdata);

would be the best course of action. The method already exists, it takes into account the LD and RD, and if any changes are made to the method in the future, you won’t have to edit your plugin.

Profile
 
 
Posted: 06 July 2008 03:30 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  07-02-2007
Justin Hurlburt - 06 July 2008 03:27 PM

Greetings,

Using:

$TMPL->swap_var_single('my_variable', $replace, $TMPL->tagdata);

would be the best course of action. The method already exists, it takes into account the LD and RD, and if any changes are made to the method in the future, you won’t have to edit your plugin.

You forgot one thing, It doesn’t do arrays smile

 Signature 

Truly ExpressionEngine


Fielder Module ( Mass Custom Fields )
Super Cache (Cache heavy tag output)
reCAPTCHA Extension
Rewrite Module


See all my EE Addons (8)

Profile
 
 
Posted: 06 July 2008 03:30 PM   [ Ignore ]   [ # 5 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

EDIT-Add Weird the posts above only appeared after I posted. I had refreshed to see if there were any new ones.

Hi Victor,

Thanks for that. Was just trying to do things with the documentation. Thought that as the function was provided that it would be best to do it this way instead of my own way. I guess this isn’t the case then?

Just wondering now how much of the documentation is gospel and how much isn’t? Having a hard time today trying to figure out what is right and what is wrong or more closely what is best.

Thanks for the information though.

Best wishes,

Mark

 Signature 

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

Profile
 
 
Posted: 06 July 2008 07:13 PM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1097
Joined  03-22-2006
Victor Gutierrez - 06 July 2008 03:30 PM
Justin Hurlburt - 06 July 2008 03:27 PM

Greetings,

Using:

$TMPL->swap_var_single('my_variable', $replace, $TMPL->tagdata);

would be the best course of action. The method already exists, it takes into account the LD and RD, and if any changes are made to the method in the future, you won’t have to edit your plugin.

You forgot one thing, It doesn’t do arrays smile

You forgot one thing, you might not want it to smile

Now, let’s say you have a date variable (read unix) and need it formatted differently on each iteration of your array, do we run complex date parsing functions to get it back to a convertible format? Make an unnecessary copy of the original array? or do we merely write a very simple foreach for the variable array?

 Signature 

(a.k.a the_butcher)

Profile
 
 
Posted: 06 July 2008 07:32 PM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  716
Joined  07-02-2007
Daniel Walton - 06 July 2008 07:13 PM
Victor Gutierrez - 06 July 2008 03:30 PM
Justin Hurlburt - 06 July 2008 03:27 PM

Greetings,

Using:

$TMPL->swap_var_single('my_variable', $replace, $TMPL->tagdata);

would be the best course of action. The method already exists, it takes into account the LD and RD, and if any changes are made to the method in the future, you won’t have to edit your plugin.

You forgot one thing, It doesn’t do arrays smile

You forgot one thing, you might not want it to smile

Now, let’s say you have a date variable (read unix) and need it formatted differently on each iteration of your array, do we run complex date parsing functions to get it back to a convertible format? Make an unnecessary copy of the original array? or do we merely write a very simple foreach for the variable array?

We are talking about single variables raspberry
The swap_var_single can’t do date variables either smile
For that you need: $LOC->decode_date($date_format, $some_var)

Hi Victor,

Thanks for that. Was just trying to do things with the documentation. Thought that as the function was provided that it would be best to do it this way instead of my own way. I guess this isn’t the case then?

Just wondering now how much of the documentation is gospel and how much isn’t? Having a hard time today trying to figure out what is right and what is wrong or more closely what is best.

Thanks for the information though.

Best wishes,

Mark

Well all i am saying is some functions can be done non-ee way, since the overhead of an EE way is unneeded like in this case. (Since the $TMPL->swap_var_single() is using str_replace anyway.
So i recommend that in this case you can just use str_replace if all you want to do is replace a variable with it’s final value.

Like Daniel said. In some cases you want to use the EE way..but i learned that sometimes your own way is faster.

P.S. Another one: $DSP class. I stopped using it cause it’s too much trouble and the extra overhead is not worth it..Straight HTML for me !!

 Signature 

Truly ExpressionEngine


Fielder Module ( Mass Custom Fields )
Super Cache (Cache heavy tag output)
reCAPTCHA Extension
Rewrite Module


See all my EE Addons (8)

Profile
 
 
Posted: 07 July 2008 01:27 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  246
Joined  04-12-2008

Well all i am saying is some functions can be done non-ee way, since the overhead of an EE way is unneeded like in this case. (Since the $TMPL->swap_var_single() is using str_replace anyway.
So i recommend that in this case you can just use str_replace if all you want to do is replace a variable with it’s final value.

Like Daniel said. In some cases you want to use the EE way..but i learned that sometimes your own way is faster.

You might be right for some cases, but here it’s just one function call more which should not be any trouble in terms of performance.

The method already exists, it takes into account the LD and RD, and if any changes are made to the method in the future, you won’t have to edit your plugin.

As this is also a reasonable answer, if you’re developing extensions for a certain application you should tend to use their API and functions in terms of ensured compatibility after possible future changes to the system.

 Signature 

Designchuchi | Twitter

URL Field Extension
Required Category Extension
DC FreeForm GeoIP Extension

Profile
 
 
Posted: 07 July 2008 04:20 AM   [ Ignore ]   [ # 9 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6543
Joined  04-15-2006

Well looks like I’ve opened a can of something here wink

Thanks for all the advice on this guys. Not really sure what is best now but would probably prefer to go with the documented functions for the reasons that peschehimself has mentioned. If I see any reason or need for arrays and can’t do it the way I need then I would go with your suggestion Victor so thanks for the information there, very much appreciated.

I’m off to try and learn some more now, wish me luck!

Best wishes,

Mark

 Signature 

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

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: 64915 Total Logged-in Users: 22
Total Topics: 81870 Total Anonymous Users: 16
Total Replies: 440142 Total Guests: 155
Total Posts: 522012    
Members ( View Memberlist )
Newest Members:  Suman KumarsmilepolitelyrvmcleodbjmohrAqua193Bios Elementmjpoteetguimogranwelshmrcf