Egor Khmelev, , (+7 905) 707-07-39

2.05

Different layout for every page in Joomla 1.5.x

Posted under Routine, 15 comments

The good thing is control layout of pages by administrator panel.
The solution is create different templates. But keeping the same things like header, footer and menu in every template is bad and if we are going to change something in future (who knows?) it will be a big problem.

So we need to have one main template with basic layout and another (inherited) templates with layout for each page.

Solution

First of all we need to create main template which will be wrapper for all the next templates.

Creating global template, named it “_global”. Files that we need:

  • /templates/_global/index.php
  • /templates/_global/layout.php
  • /templates/_global/templateDetails.xml
  • /templates/_global/helper.php


1. index.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>">
  <head>
    <jdoc:include type="head" />
    <link rel="stylesheet" href="<?php echo $this->baseurl; ?>/framework/stylesheets/application.css" type="text/css" charset="utf-8">
  </head>
<body>
  <div class="header">
    Header of page
  </div>
  <div class="layout">
    <jdoc:file name="layout.php" />
  </div>
  <div class="footer">
    &copy; Egor Hmelyoff
  </div>
</body>
</html>

Here we’ve written the new construction <jdoc:file name=”layout.php” />. Which means that you include layout.php from the current template. After that, we will create this file in each new template and insert there our layout.

2. layout.php (for _global template):

<div class="content">basic layout</div>

Here we can write whatever we need.

3. templateDetails.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE install PUBLIC "-//Joomla! 1.5//DTD template 1.0//EN"  
 "http://dev.joomla.org/xml/1.5/template-install.dtd">  
<install version="1.5.9" type="template">  
  <name>_global template</name>  
  <creationDate>23 April 2009</creationDate>  
  <author>hmelyoff</author>  
  <authorEmail>hmelyoff@gmail.com</authorEmail>  
  <authorUrl>http://blog.evershow.me</authorUrl>  
  <copyright>Life Fm, 2009</copyright>  
  <license>GNU/GPL</license>  
  <version>1.5.0</version>  
  <description>Global template with basic html layout</description>  
  <files>  
    <filename>index.php</filename>
    <filename>templateDetails.xml</filename>  
    <filename>helper.php</filename>
    <filename>layout.php</filename>  
  </files>         
 
  <positions>
  </positions>  
 
</install>

Here nothing special.

4. helper.php: (updated)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
  defined('_JEXEC') or die('Restricted access');
 
  function global_render($_params = null)
  {
    global $mainframe;
 
    // Base instances
    $document =& JFactory::getDocument();
    $user     =& JFactory::getUser();
 
    // Define global template
    $_template	= "_global";
    $_template_dir	= JPATH_THEMES.DS.$_template;
 
    // Define _global params
    if (is_readable( $_template_dir.DS.'params.ini' ))
    {
      $_content = file_get_contents($_template_dir.DS.'params.ini');
      $_global_params = new JParameter($_content);
    }
 
    // Define empty params if not exist
    if(!$_params){
      $_params = new JParameter('');
    }
 
    // example includes/application.php "render" function
    $file	= JRequest::getCmd( 'tmpl', 'index' );
    if ( JSite::getCfg('offline') && $user->get('gid') < '23' ) {
      $file = 'offline';
    }
    if ( !is_dir( $_template_dir ) && !JSite::getCfg( 'offline' ) ) {
      $file = 'component';
    }
 
    $params = array(
      'template' 	=> $_template,
      'file'		=> $file.'.php',
      'directory'	=> JPATH_THEMES
    );
 
    // Catch current template
    $template = $mainframe->getTemplate();
    $template_dir = JPATH_THEMES.DS.$template;
 
    // Set global template as current and render document
    $mainframe->setTemplate( $_template );
    $data = $document->render( JSite::getCfg('caching'), $params );
 
    // Set back current template
    $mainframe->setTemplate( $template );
 
    $replace = array();
    $matches = array();
 
    // Catch own construction for include files
    // (example /libraties/joomla/document/html/html.php "_parseTemplate" function)
    if( preg_match_all( '#<jdoc:file\ name="([^"]+)".*\/>#iU', $data, $matches ) ){
      $matches[0] = array_reverse( $matches[0] );
      $matches[1] = array_reverse( $matches[1] );
 
      $count = count( $matches[1] );
 
      for( $i = 0; $i < $count; $i++ ){
        $inc = $matches[1][$i];
        $tpl_dir = $template_dir;
 
        // if file does not exist in current template,
        // looking for it in "_global" teamplate dir
        if( !file_exists( $template_dir.DS.$inc ) ){
          $tpl_dir = $_template_dir;
        }
 
        $document->params = $_params;
        $document->global_params = $_global_params;
 
        $tpl = $document->_loadTemplate( $tpl_dir, $inc );
        $tpl = $document->_parseTemplate( $tpl );
 
        $replace[$i] = $tpl;
 
      }
 
      $data = str_replace( $matches[0], $replace, $data );
 
    }
 
    return $data;
  }
 
?>

This is the main function to render new inherited templates.
So, we’ve created “_global” template, but remeber: this template not for assign it.

Inherited templates

Next step: we will create new inherited template for home page (for example).

Files that we need:

  • /templates/home/index.php
  • /templates/home/layout.php
  • /templates/home/templateDetails.php

home” here for example, we can give any name to inherited templates.

1. index.php: (updated)

1
2
3
4
5
<?php
  defined('_JEXEC') or die('Restricted access');
  include JPATH_THEMES.DS.'_global/helper.php';
  echo global_render($this->params);
?>


2. layout.php:

1
2
3
4
<div class="content">
  <div class="left">new left column in home template</div>
  <div class="center">content part</div>
</div>


3. templateDetails.php: nothing special again.



That’s all. Now we can assign this template by Joomla administrator panel.
Also, in inherited template we have access to params of _global template by $this->global_params->get(…)

Download

Two blank templates (_global and inherited).

Summary

  1. Any count of inherited templates.
  2. Redefining modules will be in context of templates (right way).
    For example we need to redefine layout for mod_mainmenu module for all pages, add file default.php to /templates/_global/html/mod_mainmenu/default.php. Or we need to redefine layout for mod_latestnews just in home page layout, add file default.php to /templates/home/html/mod_latestnews/default.php.

  3. All controled by administrator panel.

Feel free to mail me anytime if you have any problem or question.

15 comments

Blake, 20.05 at 10:51 pm

Works great! Simple and easy. Except I’m not seeing my modified component.php, which is in the _global root folder. Also the _global params.ini is read instead of the inherited template params.ini. Any ideas?

Egor Hmelyoff, 20.05 at 11:35 pm

Thank you for your reply Blake.

Till you wrote I didn’t know about that problem, but now its solved.
Update your files and you can use in inherited template typical construction like $this->params->get(…) and also $this->global_params->get(…) for access to params of _global template.

Sorry, but I don’t understand about component.php, you can mail me with sources and I’ll try to help you.

Blake, 21.05 at 5:51 pm

Sorry, I was trying to think at the end of the day. I’m not seeing my com_content modification in the global folder. The only fix I could come up with was to create a symlink from home/html/com_content to global/html/com_content.

Seth, 11.08 at 10:02 pm

Do you know if there is a way to have a new Article automatically assume a template? Rather than having to assign a template to the article after the article has been created?

Egor Hmelyoff, 11.08 at 11:49 pm

You just need to declare and assign this template to menu items that you need. All new articles will have this template view.

Seth, 12.08 at 12:02 am

I tried editing the _global/layout.php file, but anything I put in there does not show up on the site.

Egor Hmelyoff, 12.08 at 12:25 am

First of all “_global” template not for assign. You need to edit layout.php in “home” template for example. And besides, if you have layout.php in “home” template its mean that you redefined _global/layout.php, so any changes in _global/layout.php never come up.

Seth, 12.08 at 12:34 am

Ahhh…gotcha. Thanks.

Jason, 1.10 at 5:52 pm

Hi Egor,
it seems there is no chance to create com_* overrides with this approach,
at least when I create /templates/home/html/com_content/default.php it doesn’t override default com_content component layout. Is there any solution to make it working or I do something wrong? Please help..

Jason, 1.10 at 6:12 pm

Sorry, I meant default override. Like /templates/_global/html/com_content/default.php – when i do this way it doesn’t work, when I put this into theme to assign (home in this example) then all goes fine. Creating symlink is not a solution in my case… Any ideas?

Egor Hmelyoff, 1.10 at 10:36 pm

Hello Jason, could you send me please example that doesn’t work. I will try to figure out what is wrong with that.

Claudio, 8.10 at 3:41 pm

Hi Egor,
can you tell me how Joomla decides what theme to display?
That is I have a global theme layout with 3 cols content, then a sublayout with 2 cols content but every link I click displays only 3 cols layout. What is wrong?
P.S. Default joomla theme is subLayout.

also Egor, 16.03 at 7:46 pm

Hi,
I’m studying the subject right now. As far as I understand, it could be solved using Page Class Suffix. Am I right?

Egor Khmelev, 16.03 at 7:53 pm

@also Egor, as I understand Page Class Suffix allow you to restyle html that you already have by adding a new css class. The way here explained is how to create completely new html layout for different pages.

vsree, 29.05 at 4:42 pm

hi,
I have one problem.
For my project i am using joomla CMS v1.5 for content management. Below is i mention my project structure.

In CityPortal(root folder) folder we have different folder based on cities.

–css folder
–js folder
–images folder
–index.php file
templatexml.xml file
–city one folder
—–city one index.php file
—–city one info.php file
–city two folder
—–city two index.php file
—–city two info.php file
–city three folder
—–city three index.php file
—–city three cityinfo.php file
here, i can’t access every page and i am getting error like ‘Restricted access’. Please tell me any one how to access(create) above structure using JOOMLA 1.5v.
thank for the help in advance.

   

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">