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 😉
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 😉
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! 😉
Best wishes,
Mark
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!! 😉
Thanks.
Best wishes,
Mark
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.
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
Greetings, Using:You forgot one thing, It doesn’t do arrays 😊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.$TMPL->swap_var_single('my_variable', $replace, $TMPL->tagdata);
You forgot one thing, you might not want it to 😊
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?
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.
Well looks like I’ve opened a can of something here 😉
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
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.