#1 2015-05-25 12:47:33

wrongassumption
Member
Registered: 2015-04-03
Posts: 4

Problem in passing data from controller to view in module

Dear all,
I've been working with Ionize for a while and I find it great.
I'm now rewriting an old website as a ionize module to have all the functionalities embedded in a "platform" module and leverage on the Ionize CMS and blog.

However I'm stuck on a problem when trying to pass data from a module controller to a view. More specifically, here's what i've done:

1) Since I want the module to have the site template (header, footer etc) I load and initialize the necessary elements in the constructor. The platform controller looks like this

class Platform extends My_Module
{
	public function __construct()
	{
		parent::__construct();		
		
		// --------------------------------------------------------------------------
		// To use the site menus and graphics in the module
		// --------------------------------------------------------------------------
		$this->load->library('structure');	
		$this->load->library('widget');

		// Models
		$this->load->model('menu_model', '', TRUE);

		// Add path to installed modules
		$installed_modules = Modules()->get_installed_modules();

		foreach($installed_modules as $module)
			if (isset($module['folder'])) Finder::add_path(MODPATH.$module['folder'].'/');

		// Menus
		Settings::set('menus', $this->menu_model->get_list());

		// Lang dependant settings for the current language : Meta, etc.
		Settings::set_settings_from_list($this->settings_model->get_lang_settings(config_item('detected_lang_code')), 'name','content');

		require_once APPPATH.'libraries/Tagmanager.php';
		
		TagManager_Page::init( );
		// --------------------------------------------------------------------------
		// --------------------------------------------------------------------------
		
	}

	function index()
	{
		echo $this->uri->uri_string();
		$this->template['title'] = 'Platform module title';
		$this->template['test']  = 2;
		$this->output('platform');
		$this->render('platform');
		
	}

	public function test()
	{
		echo 'Yo <br>';
		echo $this->uri->uri_string();
		$this->template['title'] = 'test';
		
		//$this->output('test');
		$this->render('test');
	}
	

}

Everything looks great until i start passing data from the controller to the view: say for instance I want to a title string or a test value:

function index()
	{
		echo $this->uri->uri_string();
		$this->template['title'] = 'Platform module title';
		$this->template['test']  = 2;
		$this->output('platform');
		$this->render('platform');
		
	}

they never make it to the view, which tells:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: test

The problem seem to be related to the render call because in the TagManager, if the call to  self::$ci->load->view($view, array(), TRUE); is active the data passing occur but the template is not rendered (i.e. no headers, no footer no css)


public static function render($view = NULL, $return = FALSE)
	{
		// Loads the view to parse
		$view = ($view != NULL) ? $view : self::$view;

		log_message('debug', 'Tagmanager::render() : Render of the view : ' . $view);

		$parsed = Theme::load($view);

		// We can now check if the file is a PHP one or a FTL one
		if (substr($parsed, 0, 5) == '<?php')
		{
			$parsed = self::$ci->load->view($view, array(), TRUE);
		}
		else
		{
			$parsed = self::parse($parsed, self::$context);

			if (User()->logged_in() && Authority::can('access', 'admin') && Settings::get('display_connected_label') == '1')
			{
				$injected_html = self::$ci->load->view('core/logged_as_editor', array(), TRUE);

				$parsed = str_replace('</body>', $injected_html, $parsed);
			}
		}

		// Full page cache ?
		$page = self::registry('page');
		if (isset($page['_cached']))
		{
			/*
			 * Write the full page cache file
			 *
			 */
		}


		// Returns the result or output it directly
		if ($return)
			return $parsed;
		else
			self::$ci->output->set_output($parsed);

	}

Can you suggest how to solve the issue?
Thank you very much in advance.

Offline

#2 2016-05-26 17:24:09

adaliszk
Ionize Friend
From: Germany
Registered: 2010-09-20
Posts: 397
Website

Re: Problem in passing data from controller to view in module

Hello, far as I know you dont need to call the $this->render() the output should load the view file with the variables in php format. Im using only the $this->output() for loading view files in backend, it should work on frontend too (but not the best way to put a custom output instead of extending the theme view).


Webdeveloper, Programmer, Application developer, Ionize friend
@adaliszk

Offline

What's Ionize ?

Ionize is an Open Source Content Management System created by webdesigners for webdesigners.

Created and maintained by Partikule and Toopixel, Ionize wants to be the easiest and most powerful CMS.

Can I help ?

Because talent is nothing without involvement, we are looking for motivated coders and webdesigners to join the project team.

Resources

Website : ionizecms.com
Documentation : doc.ionizecms.com

Development : Ionize on GitHub
Translations : Ionize Translations