Joomla : How can we assign a different layouts to

2019-07-27 10:01发布

I am running Joomla and seeking you help for the following issue.

Lets say I have 3 layouts in my template and the layout files are named as...

index.php
index2.php
index3.php

I have 5 menu links say....

Link 1
Link 2
Link 3
Link 4
Link 5

What I am looking for is......

For Link 1, Link 4 and Link 5, I want Joomla to load the regular index.php but for Link 2 I want Joomla to load index2.php and similarly for Link 3 I want it to load index3.php.

What I mean is... How can we assign a different layouts to different menu IDs?

I know there is an inbuilt option to choose a different Template based on menu ID but I do not want to duplicate the template files just for this one function. Everything in my templates is the same just the change is in the layout depending on the Menu ID.

Kindly Help.

标签: php joomla
3条回答
▲ chillily
2楼-- · 2019-07-27 10:11

I always use include_once or for security purpose require_once, from my point of view it is a better way of programming in the template process. - What do you think ? - Example I would do this way :

(isset($_GET['Itemid']))?$itemID=$_GET['Itemid']:"";
OU POUR LES PURISTES DE JOOMLA :
$itemID=JRequest::getInt('Itemid',0);

if($itemID == '57')
{
    require_once ("index1.php");
}
if($itemID == '58')
{
    require_once ("index2.php");
}
else
{
    // template code of index.php
}
查看更多
Summer. ? 凉城
3楼-- · 2019-07-27 10:12

On the basis of your menu ID (ItemID) you can include a different index<x>.php in your main index.php, like so:

$itemID = $_GET['ItemID'];

if($itemID == '57')
{
    include index1.php
}
if($itemID == '58')
{
    include index2.php
}
else
{
    // template code of index.php
}
查看更多
Viruses.
4楼-- · 2019-07-27 10:26

Are you using a commercial template or something custom? You should be able to code your index.php so that the layout is determined by the modules loaded on the page. You then control what modules show up by the menu assignments in the module parameters. You can control the layout being displayed through CSS, Page Class Suffix, and the code on index.php.

Every module position in your template should be collapsible - meaning that if no modules are loaded to the position, it does not get added to the HTML. Use something like this:

<?php if ($this->countModules('left')) : ?>
    <jdoc:include type="modules" name="left" style="xhtml" />
<?php endif; ?>

You can also use a combination of the Page Class Suffix that you can set on the System Parameters of a menu item and CSS to control the layout of the page. I add the Page Class Suffix to the BODY tag of my templates so I can control every page individually.

First, you need to figure out which menu item you are on:

<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = $params->get( 'pageclass_sfx' );
endif;
?>

Then you need to add that to the BODY tag as an ID:

<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">

Now you can use module positions and CSS to control every page. You can achieve vastly different layouts without having to go back in and touch code.

查看更多
登录 后发表回答