0

I'm building a website using CI, HMVC and Smarty. It's my first time using HMVC and I don't understand how I can use common libraries within the modules.

I prefer using Smarty on my sites and usually it's a piece of cake: I create a wrapper for smarty, autoload it and use it in controllers where necessary. But now I need to use smarty within the module controller and I don't know how to access it. Any ideas how I can do that?

I've been researching the issue for a couple of days, with no luck.

There are some answers which I just don't get: like this one

EDIT: CI 2.1.0, HMVC 5.4, Smarty 3.1.6 (but this is irrelevant)

Community
  • 1
  • 1
Valhallen
  • 60
  • 7

2 Answers2

0

You can extend your smarty in your modules with modules. path.

Example:

class MySmartie extends Smartie {

    function __construct()
    {
        parent::__construct();

        $this->template_dir = APPPATH . "modules/client/views/templates";
        $this->compile_dir = APPPATH . "modules/client/views/templates_c";
    }
}

And load this class in your modules class constructor like this:

public function __construct()
{
   $this->load->library(['mysmartie' => 'smarty']);
}

Note: don't load smarty in config/autoload.php it may create conflict in loading.

0

Here are a couple of ways:

In application/libraries

Just put the common libraries in application/libraries folder and load using $this->load->library('my_library');

Create a common module

Another option would be to create a new module, lets say common, in which you can have folders libraries,models,helpers. Here you can put files which are common to other modules. You can then load them using $this->load->library('common/my_library');

I hope that helps.

Vikk
  • 3,353
  • 3
  • 22
  • 24
  • I tried both and it didn't work. the peculiar thing was that with the second suggestion, it actually tried to load from the application/libraries folder, but that just caused an error, since have a CI instance there using another library – Valhallen Dec 04 '11 at 16:59
  • @Valhallen Both these approaches work. In CodeIgniter HMVC, you can load a library from any module using `$this->load->library('module_name/library_name');`. If you don't specify the module name, it will load from `application/libraries`. If it's not working for you, then there's something else. Have you correctly defined module path in config? Can you show the code you are using? – Vikk Dec 04 '11 at 17:10
  • I have the smarty_wrapper library autoloaded, I tried loading it manually too. the issue seems to be that with HMVC the object is not created within the modules, it works in controllers that are placed in application/controllers – Valhallen Dec 04 '11 at 21:04