Hiya,
I’ve been starting to learn the new ExpressionEngine 2 syntax for creating add-ons and I’m not having consistent results.
I would really appreciate some help from a moderator or one of the Dev team to let me know where I’m going wrong as I’ve been scratching my head on this for ages now, following the documentation and not really seeing where my inconsistencies are coming from.
Say I have an exceptionally simple plugin called pi.test.php. In it is the following code :
<?php
class Test
{
var $return_data = "";
function __construct()
{
$this->EE =& get_instance();
// $this->EE = get_instance();
}
function mine()
{
$data = $this->EE->TMPL->tagdata;
$this->return_data = $data;
}
}
?>I have this in system->expressionengine->third_party->test, test being the folder that the plugin is residing in. As far as I’m aware this is all correct so far?
When I place this code into the template :
{exp:test:mine}This is a quick test{/exp:test:mine}My brain tells me that from what I have written above I should get back exactly what I put into the plugin. In other words almost as though I hadn’t bothered to use the plugin.
This is not so though. I get absolutely no output to the screen whatsoever. Nada, zip!!
Therefore could someone please tell me where I’m going wrong? This is an exceptionally simple example and so I’m sure I must be missing something in the documentation somewhere but I’ve been over this for the past day and a half now to absolutely no avail.
As mentioned at the top of this post I’ve been having inconsistent results because I converted an old plugin I had created for 1.x and that worked fine. This plugin is the most basic I can make it and I just can’t see why it’s not working.
If I place :
return $data;as the last line instead of :
$this->return_data = $data;then it returns the text although it’s my understanding from reading the plugin documentation (I may well be wrong on this though?) that I don’t need to do that and it should be working with the line I have supplied in my original code?
Any hints as to what I’m doing wrong would be greatly appreciated. There isn’t a lot of information in the documentation as to this sort of thing and if what I am doing above should be working then I would appreciate the documentation being updated to whatever is required in order for this to work.
As it stands at the moment this is really holding me back and I’d like to get it sorted as soon as possible. Thank you.
Best wishes,
Mark
Moved to Development and Programming by Moderator
Also in my code above I have :
$this->EE =& get_instance();
// $this->EE = get_instance();I’ve noticed that the documentation says to use this line but I’ve noticed in other peoples plugins that they use the commented out line instead. Which is correct? Are they both correct? :-(
Best wishes,
Mark
Yep, except…you can’t use __construct for a single function plugin, you have to use the plugin class name:class Test { public $return_data = ''; public function Test() { $this->EE = get_instance(); return $this->EE->TMPL->tagdata; } }
Thanks for the code there. So am I correct in thinking that say I wanted a plugin with more than one function in it that this sort of thing should work :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugin_test
{
var $return_data = "";
function __construct()
{
$this->EE =& get_instance();
}
function test()
{
$this->return_data = $this->EE->TMPL->tagdata;
}
function another()
{
$this->return_data = $this->EE->TMPL->tagdata;
}
}So I should be able to do this :
{exp:plugin_test:test}This is a test{/exp:plugin_test:test}and I should get back whatever is inside the plugin tags or am I completely misunderstanding what you said above? That has definitely been known to happen before with me 😉
Unfortunately the code I’ve shown here isn’t working either so I must definitely be misunderstanding something.
Best wishes,
Mark
P.S. Is there a Dummies book for this sort of thing that you would recommend as I’d love to learn all of this once and for all and the developer documentation (I think anyway) isn’t really that involved.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugin_test
{
var $return_data = "";
function __construct()
{
$this->EE =& get_instance();
}
function test()
{
return $this->EE->TMPL->tagdata;
}
function another()
{
return $this->EE->TMPL->tagdata;
}
}Actually I just tested your code in a folder plugin_test and a file pi.plugin_test.php like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugin_test
{
public $return_data = '';
public function Plugin_test()
{
$this->EE =& get_instance();
return $this->EE->TMPL->tagdata;
echo "Test";
}
}I then placed this in the template :
{exp:plugin_test}This is a test{/exp:plugin_test}and I don’t get anything spat out on the template.
Where am I going wrong on this? :-(
Thanks for all the help with this, it really is appreciated.
Best wishes,
Mark
Remember, a single function plugin needs to set return_data:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Plugin_test
{
public $return_data = '';
public function Plugin_test()
{
$this->EE =& get_instance();
$this->return_data = $this->EE->TMPL->tagdata;
}
}Rob I was just wondering if there is a really good book that you’d perhaps suggest for me to learn all of this from?
I know enough PHP to be dangerous so I would be looking for a book which is more or less a dummies book, as that’s what I am 😉
Also to the moderators (if you’ve even seen this post yet?) I may be wrong on this and please do tell me if I am but in the plugin documentation on this page here there is this code :
class Hello_world
{
var $return_data = "";
function __construct()
{
$this->EE =& get_instance();
$this->return_data = "Hello World";
}
}This was what I was trying for ages to do but it seems like as Rob says if you are doing a single function then you need to name it the same as the class. This is not what the documentation states as an example and perhaps this thread could be noted as a documentation error?
Changing the code from the documentation to this :
class Hello_world
{
var $return_data = "";
function Hello_world()
{
$this->EE =& get_instance();
$this->return_data = "Hello World";
}
}and all starts to magically work. I’ve been literally banging my head on this for a day and a half now and it was only thanks to Rob that I’ve ever got this working. Finally I can get on with what I was trying to do ages ago.
Please please please please please update the documentation for developers and perhaps provide more working (very important word there) examples for us to learn from.
Best wishes,
Mark
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.