|
OK, we are almost done with it, I hope you enjoyed it as much as I did. Even if you don’t continue reading, you have what you need to start your quest for building fantastic modules. By the way, you can get the mod_ranlatest-lite.zip file from the downloads section that contains the example code so far. Last thing, we can discuss is styling in somewhat more detail. One of the typical parameters accompanying various modules is the “Module Class Suffix” option. You have most likely used this option while struggling with CSS and templates hence, it is good practice to add it here. This is a standard Joomla parameter, meaning you can just add the following tag to your XML file and Joomla will handle the rest of it. <param name="moduleclass_sfx" type="text" default=""
label="Module Class Suffix"
description="PARAMMODULECLASSSUFFIX" />
Although, there is no need to handle within your code this parameter, you may need to append it to the class/id names. Here is how you would do that. $mcsuffix = trim($params->get("moduleclass_sfx")); And now, for the link you could use: # Print link and pretext print "\n<p class=\"ranlatest".$mcsuffix."\">".$pptext."</p>"; print "\n<p class=\"ranlatest".$mcsuffix."\">". "<a class=\"ranlatest".$mcsuffix."\""."href=\"".$link."\">". $rows[$ranitem]->title."</a></p>"; It gets rather confusing, isn’t it? You just want to output a simple link and the resulting code becomes so unreadable. Imagine you had to output more stuff, the spaghetti would never end. This is why we keep trying separating the code from the layout. By the way, if you have been looking at other modules, you have already observed that Joomla supports templates. OK then, let’s rearrange our code to be more good looking and be able to support templates. First of all, let’s create a standard helper.php file and put the main code in it. A single function will be in there returning an array with the various bits that are to be printed out. Again, the reason for this arrangement is to separate the functionality from the layout. You see nothing is printed out, just an array is returned. Now, the entry point of our module still is the mod_ranlatest.php file which has to look like the following: <?php # Restrict access defined('_JEXEC') or die('Restricted access'); # Just print out any errors for now JError::setErrorHandling(E_WARNING, 'echo'); # Include the local helper function only once require_once(dirname(__FILE__).DS.'helper.php'); # Call main code that resides in helper.php $link = modRandomLatestHelper::getList($params); # Get the path to the layout for the module require(JModuleHelper::getLayoutPath('mod_ranlatest')); ?> It just includes the helper.php file and executes our method in there, the parameter array is passed and the link array is returned. Then, the getLayoutPath method of the JModuleHelper class is invoked that contains the path to the template file. Well, we haven’t specified one yet nevertheless the default one is /tmpl/default.php. |