Joomla : How can we assign a different layouts to different menu IDs? - php

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.

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.

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
}

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
}

Related

Hide component div from home page in joomla 3

I have a RocketTheme Affinity template that when it hides the component the wrapper stays untouched and i want it removed also.
I only want to disable it from the home page (id=101).
In the "index.php" the bellow code calls this:
<?php
echo $section_rows->render();
?>
The div name is "section-row3" in a file called "rt_sectionrows.php"
There are also css files called style6 and tempate.css files.
Conclusion:
How do I hide certain div and class from homepage?
Try this,
<?php
$app = JFactory::getApplication();
$menu = $app->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
?>
<script type="text/javascript">jQuery(document).ready(function(){
jQuery('.section-row3').hide()
});</script>
<?php
}
?>
Add this code at the bottom of your home page. It hide that div.
Hope it helps.
You should be able to just do this with some CSS.
Go into the Joomla! Administrator, Menus, Select the menu which contains your homepage menu item and then edit it.
In the menu item, select 'Page Display'. At the bottom you will see: Page Class.
Add a space and type: homep
Then add:
.homep .section-row3 {display: none !important;}
To your Rocket Theme Custom CSS or LESS file.
Then that will just hide it with CSS, we have specified !important, so it takes priority.

How to set background on active page on included menu?

I have a menu but I'm including it on every page because I don't have to copy and paste the whole code on every page. So I'm including it by this way
require "menu.php";
This is what menu.php contains
Home
.....
News
And I want when I'm on Index page the background of the link in the menubar to be gray for example and not just for the index.php but for every other page. I know how to do it when I'm not including it, just add class and I stylize the class. But now I can't think how it could happen.
I would be appreciated if someone help me.
Thank you in advance
The way I would do it would be to add a class to your main <body> (or other parent container) tag that indicates the current page:
<body class='page-home'>
Then add classes to every single menu item:
Home
.....
News
Now you can write CSS styles that target your menu links differently depending on the current page:
.page-home .nav-home {
/** styles for the home link when you are on the homepage **/
}
Now you're handling all your styling in pure CSS, and it doesn't matter that your menu is an include. It's just up to you now to decide how much styling you want to do based on the current page and the specific menu links.
You can use this tricks:
get your file by file_get_contents() and replace your class name according to your file name.
In your menu.php file:
return
'<a class=[+index] href="index.php">Home</a>
.....
<a class=[+news] href="news.php">News</a>';
In your index.php
$strMenu = file_get_contents("menu.php");
$strMenu = str_replace('[+index]','is-active',$strMenu);
echo $strMenu;
In your news.php
$strMenu = file_get_contents("menu.php");
$strMenu = str_replace('[+news]','is-active',$strMenu);
echo $strMenu;

Dynamic Layout ( Different Layout for each Page )

I'm trying to implement different layouts in Joomla. I'm using the Alias Name for setting
different layouts for each page. I read an E-book about Joomla and there's no another method to create different layouts and or set it in the Backend Panel.
I wanted to ask, is there be another method / way to set a different page from Backend Panel for each page ? I just want to make sure it's possible in Joomla 1.5.
My Previous Method
// Get Alias Page
function getCurrentAlias(){
$menu= &JSite::getMenu();
$active= $menu->getActive();
return $active->alias;
}
After getting the Alias page name, I used a conditional statement to get different content
for each page.
<-- Header Part -->
<-- Start Content Part -->
if( $pageName == "home" ){
{{Content Home}}
}elseif( preg_match("#^(news).*$#", $pageName) ){
{{Content News}}
}...etc
<-- End Content Part -->
<-- Footer Part -->
First, I wold recommend not using 1.5, it reaches end of life next month.
however, if you are going to use 1.5, you are making it a lot harder than you need to. If you want to have significant structural differences from one page to another, you can install a template for each different structure you would like to use, then assign each template to the appropriate menu item. You would have to create menu items, even if they are in a hidden menu that is not displayed on the site.
You can also control the structure of the page using CSS and collapsible module positions. Add this so you can set a page class suffix that adds an ID to the body tag of the page, making it easy to have page specific CSS:
<?php
$menu = &JSite::getMenu();
$active = $menu->getActive();
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = $params->get( 'pageclass_sfx' );
endif;
?>
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">
Then for each of the module positions on the page, you can make them collapsible so they do not show up on the page if they are not being used:
<?php if ($this->countModules('top')) : ?><div id="top"><jdoc:include type="modules" name="top" style="xhtml" /></div><?php endif; ?>
So basically, if you don't put any modules in the "top" position it never gets put on the page. Using these 2 items in combination you can control exactly how each page looks with a single template.
When we where using Joomla! 1.5 we used an extension called "Menu Dependent Items" to load particular CSS, JS etc
Through loading the right CSS etc we where able to completely relayout any given page.
Of course with the advent of template "Styles" in Joomla! 2.5+ we no longer need it as we can assign just the style variation to the menu item.

Joomla - Change template position based on the Article

I need to change some positions on the Joomla template based on the article loaded.
Ex: I need to use different set of positions for a home page and different set of positions for inner pages. There are some common positions as well (Menu, Header, Footer,etc...).
Okay this is a bit tricky, you can get the current page name (menu name) and then make if statements for each "page" you made in the template source, i'll give an example ... Let's say you have a contact page and you want to add a position to it if the user is on the contact page ...
<?php
$currentpage = JSite::getMenu()->getActive()->name ;
// in joomla 2.5
$currentpage = JSite::getMenu()->getActive()->title;
if($currentpage == "Contact"){
echo '<jdoc:include type="modules" name="Contact" />';
}
?>
And so you have to workout your whole template and anticipate for each page, this was a simple example and it's up to you to expand it ...

Drupal 7 theming: region error

this is in my theme.info file of my Drupal theme:
regions[conf_modules] = Configurator: modules
I'm using multiple templates for different node types. In one of them I'd like this region/block to show up. So I've put this in node--configurator.tpl.php:
<?php print render($page['conf_modules']); ?>
In the Drupal administration panel I have assigned a views block to the region, but on the node--configurator.tpl.php pages, the views block is not shown. Am I using render() properly? What's going wrong here? Thanks in advance!
In node templates, $page is simply a status variable that is:
True if the node is being displayed by itself as a page.
However, you can add regions to the page for specific content types through your page.tpl.php if you like. Something like below should work if placed in page.tpl.php:
<?php
if ($node->type == 'CONTENT_TYPE') {
print render($page['conf_modules']);
}
?>

Categories