CodeIgniter Forums
Pass a variable to an autoloaded library. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Pass a variable to an autoloaded library. (/showthread.php?tid=39189)



Pass a variable to an autoloaded library. - El Forum - 03-03-2011

[eluser]DephNet[Paul][/eluser]
Hi Guys,

I have written a library that generates my page headers and footers, and have set it up to be auto loaded, but I am having a bit of trouble passing a variable to the library, the main one will be for my page title.

I have read the user guide, that only deals with a library loaded from the controller, but am stuck with this, how do you pass a variable, from a controller, to an auto loaded library?

Many thanks for your help
--Paul


Pass a variable to an autoloaded library. - El Forum - 03-03-2011

[eluser]ramm[/eluser]
Maybe with the $config array?

Can i ask why would you want to generate static data with a library instead of using a template for that?


Pass a variable to an autoloaded library. - El Forum - 03-04-2011

[eluser]DephNet[Paul][/eluser]
[quote author="ramm" date="1299224913"]Maybe with the $config array?[/quote]How would you go about referencing the config array in the library? Every time I have tried to do this I get an error about $config not being set.

[quote author="ramm" date="1299224913"]Can i ask why would you want to generate static data with a library instead of using a template for that?[/quote]Because there is alot of stuff that goes on in creating my headers, and a library is, from where I am sat, the best place for this to go.

--Paul


Pass a variable to an autoloaded library. - El Forum - 03-04-2011

[eluser]ramm[/eluser]
Well, i was thinking the same way it works for other libraries, let's say
Code:
//For a googlemaps lib i'm using
$this->load->library('googlemaps');
$config['center'] = 37.4419, -122.1419;
$config['zoom'] = 16;

$this->googlemaps->initialize($config);

//You can do the same here
$config['page_title'] = "Your title";

As in the library itself they have it like this:
Code:
class Googlemaps {
  var $center = "37.4419, -122.1419";
  var $zoom   = 13;
  //some others

  function Googlemaps($config = array())
  {
    if (count($config) > 0)
    {
       $this->initialize($config);
    }    
  }


  function initialize($config = array())
  {
    foreach ($config as $key => $val)
    {
      if (isset($this->$key))
      {
        $this->$key = $val;
      }
    }
  }

}