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"> © 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
- Any count of inherited templates.
- 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 . - All controled by administrator panel.
Feel free to mail me anytime if you have any problem or question.
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?