2

I have created an EE2 plugin under third_party folder and it succesfully shows up in Plugins list @ backend.

Now, is it possible to create an instance of that plugin class directly from a template as I don't need any template tag behaviour? Eg.

<?php
$cart = new Cart;
?>

Where as the plugin would have the following structure:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$plugin_info = array();

/**
 * Cart Class
 * ...
 */

class Cart {

    var $return_data;
    function Cart() {
    }

    ...

}
?>

OR, is there a better way in achieving this preserving the possibility to use tags later on? Thanks!

5
  • 2
    In a word, no. But can you further explain why you want to do this? Why would you want to instantiate your class, but then do nothing with it? Apr 10 '13 at 12:09
  • Derek is right. If you are creating plugin then you can use plugin tag to populate any data. Also, for passing dynamic variable within the plugin you can use parameters. Apr 10 '13 at 13:16
  • Hi and thanks for your replies! I have template files that instantiate the class in regular php-way and use class methods to get data for the template. The reason I want it to be a plugin (or something similar) is that I would like to have it out of the expression engine system directories AND have the possibility to use template tags later on. I have now placed the class under expressionengine/system/libraries and it works as expected, just want to know if there's a better or more correct way in doing this. Thanks! Apr 12 '13 at 11:30
  • I think moving forward with the plugin would be the way to go in this case. This way you don't worry about forgetting to modify the native libraries when you upgrade, etc.. Then you could utilize the single tag and tag pairs to retrieve whatever you needed without enabling php in templates. If you are keeping items/data in the cart class, you could create an empty object (bag of bones) and then use the tag pairs to display items or add costs. {exp:cart}{item}{/exp:cart}
    – W3bGuy
    Sep 5 '13 at 17:01
  • You might also want to note that using PHP in templates is highly inefficient. If you're doing alot of PHP in the templates, you'll find your performance will suffer in comparison to using the plugin's template tags. Nov 7 '13 at 1:37
1

It is generally bad practice to have php in your template. It isn't recommended by Ellis Lab and is not a good idea to even allow php in your templates in template preferences.

I hope this helps:

Here is my plugin starter kit. I hope this helps this will give you a plugin set up in a way that is able to be expanded on to add more functions and such.... to use the public construct it would be {exp:pluginname} if you added a function it would be {exp:pluginname:function} You will want to save this as pi.pluginname.php in a pluginname folder in the third-party folder.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Pluginname Class

 * @package         ExpressionEngine
 * @category        Plugin
 * @author          Matthew Johnson
 * @copyright       Copyright (c) 2010, Jane Doe
 * @link            http://midwesterninteractive.com/
 */

$plugin_info = array(
    'pi_name'         => 'Pluginname',
    'pi_version'      => '1.0',
    'pi_author'       => 'Matthew Johnson',
    'pi_author_url'   => 'http://MidwesternInteractive.com/',
    'pi_description'  => 'Allows you to generate 1 of 13. ',
    'pi_usage'        => PluginName::usage()
);

class Pluginname
{

    public $return_data = "";

    // --------------------------------------------------------------------

    /**
     * Pluginname
     *
     * Plugin Description
     *
     * @access  public
     * @return  string
     */
    public function __construct()
    {
        $this->EE =& get_instance();   

    }

    // --------------------------------------------------------------------

    /**
     * Usage
     *
     * This function describes how the plugin is used.
     *
     * @access  public
     * @return  string
     */
    public static function usage()
    {
        ob_start();  ?>

        @mattsidjohn
        @buildmidwestern
        http://midwesterninteractive.com


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

        return $buffer;
    }
    // END
}
/* End of file pi.pluginname.php */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.