Welcome Guest, Not a member yet? Register   Sign In
Calling 'require_once' from a controller
#5

[eluser]thekrow[/eluser]
Hi Pert, maybe if I just paste my two functions you'll be able to understand this better. What happens is, I...

1. get my language from an array contained in one of my libraries (first one is the default language)
2. load content files
3. get variables from the files and assign them to be passed into my view
4. Display template based on input

If I copy/paste the content of load_files() into index() then it works. The only thing I did was to call a function instead of having all those include() calls in my index(). The reason I want to load them in a separate function is because I want to convert load_files() into a helper function, basically because I load the bunch of files (the ones I'm calling with 'include') in different controllers. It's better for me to just call load_files($language,$page) in my controllers and not having of that bunch of require functions every time I need them. I hope I'm clear.

Code:
function index($page = 'home')      
{    
  //not_found($page);        // Quit if page is not found

  $language = $this->multilanguage->language;  // Select either default or set new lang depending on dropdown choice
  
  // Load files
  $this->load_files($language,$page);  
  
  // Assign variables
  $this->smarty->assign('url',$this->multilanguage->url_to_be_assigned($page)); // URL assigned for switching language
  $this->smarty->assign('page',$page.'.tpl');    
  $this->smarty->assign('title',title($page));    // title() helper used
  $this->smarty->assign('meta',meta($meta[$page]));   // meta() helper passes array of meta tags based on input page  
  $this->smarty->assign('top',$top);      
  $this->smarty->assign('category',$category);  
  $this->smarty->assign('right_content',$right_content);
  $this->smarty->assign('footer',$footer);    
  $this->smarty->assign('data',$data);      // Main content
  
  ob_start();
   isset($_SESSION['poll_result']) ? $this->poll_result() : $this->poll_item();
   $this->smarty->assign('poll', ob_get_contents());
   unset($_SESSION['poll_result']);
  ob_end_clean();
  
  // Display template based on input
  $this->display_template($this->select_template($page));  

}

function load_files($language,$page)
{
  include APPPATH.'language/'.$language.'/'.str_replace('-','_',$page).'_lang.php';
  include APPPATH.'language/'.$language.'/includes_mid_content_lang.php';  
  include APPPATH.'language/'.$language.'/includes_top_lang.php';    
  include APPPATH.'language/'.$language.'/includes_right_content_lang.php';  
  include APPPATH.'language/'.$language.'/includes_footer_lang.php';    
  include APPPATH.'language/'.$this->multilanguage->select_meta_file($language);
}

[quote author="Pert" date="1370437947"]Odd, I've definitely messed about with $this in script included in the middle of the class method.

Maybe try just setting the variable before include?

Code:
class xxx
{
   function yyy()
   {
      var $top;
      include 'other-file.php';
      ...
   }
}

[/quote]


Messages In This Thread
Calling 'require_once' from a controller - by El Forum - 06-04-2013, 01:15 PM
Calling 'require_once' from a controller - by El Forum - 06-05-2013, 01:25 AM
Calling 'require_once' from a controller - by El Forum - 06-05-2013, 05:39 AM
Calling 'require_once' from a controller - by El Forum - 06-05-2013, 06:12 AM
Calling 'require_once' from a controller - by El Forum - 06-06-2013, 02:42 PM
Calling 'require_once' from a controller - by El Forum - 06-06-2013, 05:41 PM
Calling 'require_once' from a controller - by El Forum - 06-07-2013, 01:18 AM
Calling 'require_once' from a controller - by El Forum - 06-07-2013, 01:19 AM
Calling 'require_once' from a controller - by El Forum - 06-07-2013, 09:02 AM
Calling 'require_once' from a controller - by El Forum - 06-07-2013, 09:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB