The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 16 posts ] 
Author Message
PostPosted: Thu Jul 05, 2007 9:50 am 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Tue Sep 19, 2006 9:12 am
Posts: 91
Location: Hampshire, UK
Hi,

I was wondering if anyone could help out with a little problem.

I am developing a couple of custom components for J1.5 . On some views of some of the components, I want a parameter selection / search form to allow users to adjust the date range and users for which they want to see data. What I would really like to do is to have this search form displayed by a module so that I don't have to replicate the code across the various components. Since the components that show the searched-for data are reliant on the presence of the search form module, I want them to have control over if it displays or not, rather than set the module to be published on certain pages via the admin screen.

So, with this in mind, how can I cause a module to render within a component?

I tried adding

to my controller, then view, then default.php in the template folder, and all to no avail, so I assume I'm missing something vital... if what I want to achieve is even possible. Would I need to set the module to publish on the admin side of things even if I'm directly calling the render function (or something else that would work better)? Any help would be much appreciated. Thanks in advance.

Hello
:-Lightinthedark

_________________
Hello
:-Lightinthedark


Last edited by lightinthedark on Fri Jul 06, 2007 9:16 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Thu Jul 05, 2007 10:22 am 
User avatar
Joomla! Guru
Joomla! Guru

Joined: Thu Nov 10, 2005 10:08 am
Posts: 821
I haven't tried this myself, but you maybe you need to add echo:[php] 
echo JModuleHelper::renderModule('mod_mymodule');
[/php]

_________________
Better SEO & multi-lingual Joomla sites with Nooku Content
http://www.nooku.org
Nooku Framework for advanced Joomla extension development
http://www.nooku.org/framework


Top
 Profile  
 
PostPosted: Thu Jul 05, 2007 10:48 am 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Tue Sep 19, 2006 9:12 am
Posts: 91
Location: Hampshire, UK
I have now tried that and it made no difference :(

To be sure that my module wasn't doing anything odd, I changed to using the statistics module from the sample install data. I have the following in my default template file, and have tried putting the middle 2 lines in the view or the controller too
Code:
<?php
      $tmp = JModuleHelper::renderModule('mod_stats');
      echo '<pre>';var_dump($tmp);echo'</pre>';
?>

The only output this generates is:
Quote:
NULL

So I guess that doesn't work as hoped. The statistics module is published, and set to display on selected menu items but with none selected. If I set the module to display on the same menu link as the component that's trying to do the direct render, the module displays in its regular position (as it would without the above code), but not in the component area as I want it to.

Any thoughts?

Hello
:-Lightinthedark

_________________
Hello
:-Lightinthedark


Top
 Profile  
 
PostPosted: Thu Jul 05, 2007 10:58 am 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Mon Dec 12, 2005 5:34 pm
Posts: 418
Hi,

the $module parameter of renderModule() is supposed to be an object not a string.
(/me making a mental note to update the API docs..)
Code:
<?php

$module = &JModuleHelper::getModule('mod_stats');
$output = JModuleHelper::renderModule($module);
...
echo $output;


The module must be published, or JModuleHelper::getModule() won't return a thing.

Hope this helps,
CirTap

_________________
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams


Top
 Profile  
 
PostPosted: Thu Jul 05, 2007 5:12 pm 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Tue Sep 19, 2006 9:12 am
Posts: 91
Location: Hampshire, UK
Hi,
Thanks for the help so far, but sadly it still doesn't work. My code now has:

Code:
   $module = &JModuleHelper::getModule( 'mod_stats' );
   echo '<pre>';var_dump($module);echo'</pre>';
   $output = JModuleHelper::renderModule( $module );
   echo '<pre>';var_dump($output);echo'</pre>';


and those "var_dump"s give me:
Quote:
NULL
string(0) ""

... which is not really what I was hoping for.
I have the latest version of the code from the SVN (rev 7869), fresh install with sample data (so the mod_stats module is def. in there), have tried this with legacy mode on and off (just in case), with the module published, set to display on no pages and then on all pages. Even with it set to display on all pages it still doesn't come up where that call is, only in its regular slot on the left panel where it would come up anyway.
Since the first var_dump shows a NULL, I guess the module isn't being got by getModule. I tried changing the 'mod_stats' to 'Statistics' (the title of the module instead of the mod_type), but that made no difference. Does anyone else get the same results if they put that code into a views/view_name/tmpl/default.php files like I have it?

Hello
:-Lightinthedark

_________________
Hello
:-Lightinthedark


Top
 Profile  
 
PostPosted: Thu Jul 05, 2007 5:57 pm 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Mon Dec 12, 2005 5:34 pm
Posts: 418
jeez... my bad.
this is what happens with copy'n'paste :)

drop the 'mod_' prefix:
Code:
<?php
$_module = &JModuleHelper::getModule( 'stats' );
echo '<xmp>', print_r($_module, true), '</xmp>';
$output = JModuleHelper::renderModule( $_module, array('style'=>'xhtml') );
echo '<xmp>', print_r($output, true), '</xmp>';


CirTap

_________________
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams


Top
 Profile  
 
PostPosted: Fri Jul 06, 2007 9:15 am 
User avatar
Joomla! Intern
Joomla! Intern

Joined: Tue Sep 19, 2006 9:12 am
Posts: 91
Location: Hampshire, UK
Fantabulous.
If I have the module published and set to display on all menu items, but set to a position that isn't on the template then I get the module showing only where I call it with your code  :D  It'd be nice if I didn't have to set the module to show on every menu item, but I can live with that tiny bother.

Thanks for all the help, now I have no excuse to dodge my work  :laugh:

Hello
:-Lightinthedark

_________________
Hello
:-Lightinthedark


Top
 Profile  
 
PostPosted: Fri Jul 06, 2007 1:00 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Fri Mar 02, 2007 1:57 pm
Posts: 15
Strangest thing....

I JUST came on to this site to see if this was possible and hey presto, my answer was sitting at the top of the Joomla! 1.5 Developer Forum Activity list!!!

:D :D :D :laugh:


Top
 Profile  
 
PostPosted: Fri Jul 06, 2007 1:27 pm 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Mon Dec 12, 2005 5:34 pm
Posts: 418
[quote=∓quot;Andrew Martin"\]
One is glad to be of service.
[/quote]

_________________
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams


Top
 Profile  
 
PostPosted: Mon Jul 09, 2007 1:05 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Apr 25, 2007 10:23 am
Posts: 9
Location: Athens, Greece
As Dr. Henry Jones exclaimed in Indiana Jones & the Last Crusade,

"...and the solution presents itself" ;-)

Brilliant.


Top
 Profile  
 
PostPosted: Tue Jul 10, 2007 11:00 am 
Joomla! Explorer
Joomla! Explorer

Joined: Sun Jun 11, 2006 10:51 am
Posts: 265
Location: Finland
One more possibility,

because component is dispatched before modules are rendered, if template would be replaced by template having instead tag it content, modules would be rendered automatically... Replace would be done with plugin onAfrerDispatch.

Only guestion, can template be replaced (or how it can be replaced) before module rendering starts.

_________________
http://typicalprogrammer.com/?p=8


Top
 Profile  
 
PostPosted: Wed Jul 11, 2007 9:46 pm 
User avatar
Joomla! Champion
Joomla! Champion

Joined: Fri Aug 12, 2005 12:47 am
Posts: 6568
hannul wrote:
because component is dispatched before modules are rendered, if template would be replaced by template having instead tag it content, modules would be rendered automatically... Replace would be done with plugin onAfrerDispatch.


Not sure I completely understand what you mean here, can you try again ? :)

_________________
Johan Janssens - Joomla Co-Founder, Lead Developer of Joomla 1.5

http://www.joomlatools.eu - Joomla extensions that just work
http://www.nooku.org - Extension development framework for Joomla


Top
 Profile  
 
PostPosted: Thu Jul 12, 2007 7:08 am 
Joomla! Explorer
Joomla! Explorer

Joined: Sun Jun 11, 2006 10:51 am
Posts: 265
Location: Finland
Ok, this is code example.

Tested it and it works. Ok, not beautiful solution, and maybe not the best solution  :D

Code:
class CheckoutController extends JController
{

   /**
    * Controller for testing module inside component
    *
    */
   function viewLogin()
   {
      $view =& $this->getView( 'login');
      $model   =& $this->getModel( 'login' );
      $view->setModel( $model, true );
      $view->display();
      $dispatcher =& JEventDispatcher::getInstance();
      $dispatcher->register('onAfterDispatch', 'prepare_inline_modules');
      //and should also register task to unlink created temporary file.
   }


}

function prepare_inline_modules(){

#These are called when dispatching:
#JDocumentHTML::render
/*
$this->template =& $template;

// load
$data = $this->_loadTemplate($directory.DS.$template, $file);

// parse
$data = $this->_parseTemplate($data);
*/

#As we all know, this code sets components output to buffer.
#components output can then be retrieved by:

$document   =& JFactory::getDocument();
$buffer = $document->getBuffer('component');

#Then modules are rendered afterwards, starting when mainframe->render is called and rendering executed.
#Of course here template is loaded. Now there is a change after dispatch to inject new templatefile, that allready has components rendered content that has module tags.

#like this:
global $mainframe;
$temptemplatename = md5("something here or what ever");
//get real template.
$template   = $mainframe->getTemplate();
$file       = JRequest::getCmd('tmpl', 'index');
$data = file_get_contents(JPATH_THEMES.DS.$template.DS. $file.".php");

//and preg_replace
$newtemplatedata = preg_replace('#<jdoc:include\ type="component".*?\/>#i', $buffer, $data);

//save it, and set saved file to be new rendered template

$h = fopen(JPATH_THEMES.DS.$template.DS.$temptemplatename.".php",'w');
fwrite($h,$newtemplatedata);
fclose($h);
JRequest::setVar('tmpl',$temptemplatename);

}

_________________
http://typicalprogrammer.com/?p=8


Top
 Profile  
 
PostPosted: Fri Jul 13, 2007 1:11 am 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Mon Dec 12, 2005 5:34 pm
Posts: 418
.. interesting ... rough, and lots of file I/O :)
how 'bout a cache?

I guess this, or something equally simple, is effective enough
  $_module = &JModuleHelper::getModule( 'stats' );
  $output = JModuleHelper::renderModule( $_module );
and it's frighting flexible -- given the module(s) in question are enabled.

For content there is a {loadposition} plugin, and any other component that wants to support "Inline Module Rendering" (IMR™) can use the same or a similar technique as com_content; template variables are an option, I guess.

My € 2ct

CirTap

_________________
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams


Top
 Profile  
 
PostPosted: Fri Jul 13, 2007 7:46 am 
Joomla! Explorer
Joomla! Explorer

Joined: Sun Jun 11, 2006 10:51 am
Posts: 265
Location: Finland
That was just another example, even I don't think that is an elegant solution. Those file io's and cachable issues though are solvable.

Plugins don't actually do any less io's or solve/create any more caching issues. Only that one filewrite is avoided, and content can be cached, so even that can be avoided. 

What I always try to avoid is using solutions that brake when core is changed.

What would be the ultimate solution is one simple core interface, that would enable to load any module within component, also by its name, not only by position. Or/And possibility to leave to dispatched content instruction to later do some module rendering by the core.

Love the different possibilities that openup with J design.

_________________
http://typicalprogrammer.com/?p=8


Top
 Profile  
 
PostPosted: Fri Jul 13, 2007 12:06 pm 
User avatar
Joomla! Explorer
Joomla! Explorer

Joined: Mon Dec 12, 2005 5:34 pm
Posts: 418
... I was not bashing your idea, but was just teasing :)

As you demonstrated yourself, the Framework is very flexible, it's just the Applications "limiting" some of its features and demand a specific workflow -- that's what applications usually do. In case of the Joomla! CMS, one "artificial" limitation is the Module model, bound to the site templates.
Nobody and nothing restricts a component developer to implement its own plugin/module system to enhance and extend the component.

Quote:
Love the different possibilities that openup with J design

yep, it'll be interesting to see what the "next generation" of extensions will come up with  :-*

Have fun,
CirTap

_________________
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 



Who is online

Users browsing this forum: No registered users and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group