Page 1 of 1
Running modules via code
Posted: Mon Dec 12, 2005 8:01 am
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.
Re: Running modules via code
Posted: Mon Dec 12, 2005 10:40 am
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:
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!
Re: Running modules via code
Posted: Mon Dec 12, 2005 12:27 pm
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.
Re: Running modules via code
Posted: Mon Dec 12, 2005 1:36 pm
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!
Re: Running modules via code
Posted: Tue Dec 13, 2005 2:10 am
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.