Moving from <jdoc> to PHP
The code in this tutorial could be placed in your template. It will produce the HTML of a menu, so this allows you to migrate from the <jdoc> tag to PHP-code. Take for instance the following example:
<jdoc:include type="modules" name="top" style="xhtml" />
We will assume that the only reason for this position "top" is to allow a menu-module to be configured at this spot. It is a horizontal menu with about four or five menu-items, and is based on a <div> tag (that's why the "style" is set to "xhtml"). The menu-module needs to be configured to have a startlevel of 0 and an endlevel of 1. This means it will only include the uppermost menu-items. As menu-name we choose globalmenu which is our main menu-structure (but it could also be mainmenu).
Now let's put together the PHP-code. First of all we are going to include some code at the top of the template, just after the _JEXEC check.
<?php
defined( '_JEXEC' ) or die( "Please die" );
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer( 'module' );
$topmenu_module = JModuleHelper::getModule( 'mod_mainmenu' );
$topmenu_module->params = "menutype=globalmenu\n"
. "startLevel=0\n"
. "endLevel=1";
$options = array( 'style' => "raw" );
$topmenu = $renderer->render( $topmenu_module, $options );
?>
The menu was initialized through a new module-instance $topmenu_module, which received the proper parameters startLevel and endLevel. This module-instance is parsed through a renderer to create HTML-output. The $topmenu now contains the HTML-structure of the menu.
Now we can replace our <jdoc>-tag with the following code:
<div class="module"><?php echo $topmenu; ?></div>
You can test the output in your template. It should show you the contents of your globalmenu menu.
Creating the submenu
The submenu is created in almost the same way. We initialize a module-instance $submenu_module, give it the proper parameters and render it. Note that the startLevel and endLevel change:
And to output the HTML:
We could also simulate a "rounded" style by including more <div> tags:
<div class="module"><div><div><div> <h3>Submenu</h3> <?php echo $submenu; ?> </div></div></div></div> Changing the submenu title dynamically
What you can see in the HTML-block of our submenu, is that it has a <h3> tag (which is fine) but includes a static title (which is not fine). We need some extra PHP-code to generate a dynamic title. We assume here that because the topmenu and submenu are derived from the same menu-tree, we need to fetch the item from the topmenu which is now active.
Instead of these lines we can also write the following on one line - assuming we run PHP5:
But probably it does not make it easier to understand.
The final step is to replace the static title with our dynamic variable.
And there we have our complete splitmenu functionality. It doesn't require us to rewrite the full mod_mainmenu functionality, while it gives us much more flexibility when building custom templates.
Source: Collect from internet
Developer and Project Manager: Ashik Mahmud
Odesk Hire Link:
No comments:
Post a Comment