Running modules via code

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
linsec
New Member
New Member
Posts: 7
Joined: Wed Dec 07, 2005 1:21 am

Running modules via code

Post by linsec »

Hello Devs,

How can I run a module within a module?

Basically, i want to call a module within the code of the current module, when i try something like:

          {cms_module module="modulename"}

within the module im creating, it just echo's it out on the page and doesnt run the module.

Hopefully this can be rectified using some internal class im not aware of ;)

Any assistance appreciated.

Shane.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Running modules via code

Post by Ted »

There are 2 different ways to do this...

1. If you're using smarty templates for your output (like News does), then you can put the text into a smarty varliable and eval that variable.  For instance, in your action code:

Code: Select all

$this->smarty->assign_by_ref('moduletext', '{cms_module module="modulename"}');
And then in your template:

Code: Select all

{eval var=$moduletext}
2. Actually open up the module and call it yourself...  which is a little hairy

Code: Select all

$newmodule =& GetModuleInstance('modulename');
//Make sure the id given is different, but easily reproduced
@ob_start();
$newmodule->DoAction('default', $id . '123', $params, $returnid);
$text = @ob_get_contents();
@ob_end_clean();
return $text;
Hope that helps!
linsec
New Member
New Member
Posts: 7
Joined: Wed Dec 07, 2005 1:21 am

Re: Running modules via code

Post by linsec »

Hi wishy,

Thanks for the fast response!

The first option gave an error:  Fatal error: Cannot pass parameter 2 by reference in line {blah}

Second gave an error saying that GetModuleInstance is an undefined function... :(

Am i missing something? The module im trying to run is fine when run from it's own page, it just doesnt want to work within my code.

Ill take another look in the morning, thanks for your efforts.

Shane.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Running modules via code

Post by Ted »

Yeah, I have to stop posting technical things in the morning...  :)  2 glaring mistakes there on my part.

#1 - Can't do the assign_by_ref with a string... use assign instead

Code: Select all

$this->smarty->assign('moduletext', '{cms_module module="modulename"}');
#2 - GetModuleInstance is a method of the module API, so it needs a $this...

Code: Select all

$newmodule =& $this->GetModuleInstance('modulename');
Sorry about that!
linsec
New Member
New Member
Posts: 7
Joined: Wed Dec 07, 2005 1:21 am

Re: Running modules via code

Post by linsec »

Hi Wishy,

No luck with these options, when running the following:

$id = 'myid';
$newmodule =&$this->GetModuleInstance('modulename');
@ob_start();
$newmodule->DoAction('default', $id, $params, $returnid);
$text = @ob_get_contents();
@ob_end_clean();
return $text;

I get the error:

Fatal error: Call to a member function on a non-object in {Line: $newmodule->DoAction('default', $id, $params, $returnid);}

Of course substituted myid with the module ID and modulename with the name of the module.

Any ideas?

Cheers,

Shane.
Post Reply

Return to “Developers Discussion”