Home >CMS Tutorial >WordPress >Create a template with Joomla: step by step
This tutorial will guide you through the necessary steps to create a Joomla template from scratch. We'll cover every detail; so let's get started!
This tutorial is excerpted from the recently released "Joomla! 1.6: User Guide" provided by Pearson.
Joomla templates are a series of files in Joomla CMS that control the rendering of content
Joomla templates are a series of files in Joomla CMS that control the rendering of content. A Joomla template is not a website; it is a website. It is also not considered a complete website design. A template is the basic design for viewing a Joomla website. To produce the effect of a "complete" website, the template works closely with the content stored in the Joomla database.
The template is styled so that when content is inserted, it automatically inherits the styles from the stylesheet defined in the template, such as link styles, menus, navigation, text size and color, etc.
Like Joomla, using CMS templates has many advantages:
The web page you see on a Joomla-powered website is not static; it is dynamically generated based on content stored in the database. When content in the database changes, all pages that display that content change immediately. The page you see is created through various PHP commands in the template that query the database. Some difficulties arise during the design phase since the template looks like lines of code rather than content.
There is no "right way" to create a web page.
It's now common to use "what you see is what you get" (WYSIWYG) HTML editors, such as Dreamweaver, so you don't have to write HTML code. However, it is not possible to use such an editor during Joomla template design because the WYSIWYG editor cannot display and edit dynamic pages. Therefore, you have to manually code the template and its CSS and view PHP's output page on the service page that is frequently refreshed when changes are made. If the connection is fast enough, this might be a Web server, but most designers use a local server or localhost on their computer—a piece of software that serves web pages on their computer, such as the localhost setup described in Chapter 1 2. "Download and install Joomla!"
There is no "right way" to create web pages; how you do it depends on your background. Those who prefer graphics tend to make "images" of the page in a graphics program (such as Photoshop) and then break up the images in order to use them for the web (called slicing and dicing). More technology-based designers often jump directly into CSS and start coding fonts, borders, and backgrounds. However, as just mentioned, as a Joomla template designer, you are limited by the fact that you cannot immediately see the effects of your coding in the same editor. So you can use the following modified design flow:
To continue with this tutorial, you will need to install WampServer. If you have not done so already, continue with the installation. I'll wait here.
On a hosted web server, you can edit HTML templates and CSS files on the backend while opening the frontend in another tab in the browser. When saving changes, you can simply refresh the frontend view to see the impact.
With localhost settings, you can more easily access files directly and edit them with the editor of your choice. After saving your changes, you can refresh the frontend view in your browser and see the impact without closing the editor.
Usability, accessibility, and search engine optimization (SEO) are all phrases used on the Internet today to describe high-quality web pages. In fact, there is considerable overlap between usability, accessibility, and SEO, and web pages that exhibit one of these characteristics often exhibit all three. The simplest way to achieve these three goals is to use the framework specified in the W3C Web Standards.
For example, people with low vision can easily read websites built using HTML semantics with a screen reader. It can also be easily read by search engine spiders. Google is actually blind on how to read a website; just like using a screen reader.
Web standards establish a common set of "rules" that all web browsers use to display web pages. The main organization driving these standards is the W3C, whose director, Tim Berners-Lee, is credited with inventing the Web in 1989.
To understand where web standards come from, it helps to know a little history. Many web pages are actually designed for older browsers. Why? Since the birth of the World Wide Web, browsers have continued to evolve. Each generation introduced new features, for which manufacturers came up with different and sometimes proprietary labels (names). Each browser tends to have a different syntax or "dialect" and quirks that implement the same basic HTML language. New browsers have emerged and some old browsers have disappeared (remember Netscape?).
The current W3C standards help (hopefully) push manufacturers to release more compatible browsers that can read the same languages and display pages more consistently so that designers can target Design on a single common platform.
Another complicating factor is that, historically, different browser manufacturers (such as Microsoft) have tended to have their browsers interpret HTML in slightly different ways. Therefore, web designers must design their websites to support older browsers rather than newer ones. Designers and website owners often consider it important that web pages display correctly in these "older" browsers. W3C standards for web page code are developed to achieve consistency. A website that incorporates W3C web standards has a good foundation to make itself accessible, usable, and optimized for search engines. Think of these as building codes for your home: websites built with them are stronger, more secure, and aligned with user expectations. You can check your pages using the W3C's HTML Validation Service (validator.w3.org). It's simple and free (just make sure you use the correct DOCTYPE when trying to validate your code. At its simplest, a website that meets W3C validation might also use semantic HTML and use CSS to separate its content from presentation.
Ask five designers what web standards are and you'll get five different answers. But most agree that web standards are based on using valid code, whether it's HTML (or otherwise), as specified in the latest version of the standard. Specified way.
Semantically correct means that the HTML tags in the web page only describe the content, not the representation
As mentioned before, semantically correct means that the HTML tags in a web page only describe the content, not the presentation. In particular, this means a structured organization of H1 tags, H2 tags, etc., and using tables only for tabular data , not for layout. One area where Joomla template designers slightly compromise on purely semantic correctness is naming the left and right columns of two- or three-column layouts "left" and "right" rather than semantically A more correct sidebar or sidebar. If these are just the location names used in the template PHP, then they are technically correct. If they are also used to define matching classes in HTML and CSS, then they will be consistent with the display page It is a forgivable convenience to name or classify everything related to the left column as left. In the example below, you will see that the position on the left is styled using the class sidebar, and the position on the right is sidebar- 2. This is semantically correct code.
Closely related to making code semantically correct is the use of CSS to control the appearance and layout of web pages. CSS Is a simple mechanism for adding style (such as fonts, colors, spacing) to web documents.
CSS exists side by side with HTML code, allowing you to completely separate content (code) from presentation (CSS) Separation.
To see this in action, check out CSS Zen Garden, a website that displays the same HTML content in different and unique ways simply by changing the CSS files. The resulting page looks Very different, but at the core the same thing.
Currently, designing websites powered by Joomla faces considerable challenges in meeting validation standards.
Currently, designing websites powered by Joomla faces considerable challenges in meeting validation standards. In the first series of Joomla versions, 1.0.X, the code used a lot of tables to output its pages. This isn't really a demonstration of using CSS, nor does it produce semantically correct code. This problem is compounded by the fact that many third-party developers of components and modules still use tables to generate layouts.
Fortunately, the Joomla core development team recognized this problem with Joomla. In Joomla 1.5, template designers can completely override the core's output (called views) and remove tables or customize layouts in any way they want.
Care still needs to be taken when creating a template to ensure that it is accessible (e.g., scalable font size), usable (clear navigation), and optimized for search engines (sorted source code).
To understand what the template is about, we first look at a blank Joomla template.
This section reviews the manual process of setting up a template file. Normally, you install the template using the Joomla installer, which takes care of all these steps.
When building your own template, you need to set up multiple files and folders in a coordinated manner. Templates need to contain various files and folders. These files must be placed in the /templates/
directory of your Joomla installation, with each file placed in the folder specified for that template. If you installed two templates named Element and Voodoo, your directory would look like this:
/templates/element /templates/voodoo
Note that the directory name for the template must be the same as the name of the template - in this case element and voodoo. These names are case-sensitive and should not contain spaces.
There are two key files in the template directory:
/element/templateDetails.xml /element/index.php
These file names and locations must match exactly, as this is how the Joomla core script calls them. The first is a template XML file.
This is an XML-formatted metadata file that tells Joomla what other files are required when loading a web page that uses this template. (Note the capital D.) It also details the author, copyright, and files that make up the template (including any images used).
The last use of this file is to unzip and install the template when using the extension installer on the admin backend.
The second key file is the main template file for generating pages, index.php.
This file is the most important file in Joomla template. It lays out the site and tells Joomla CMS where to place different components and modules. It is a combination of PHP and HTML.
Almost all templates use additional files. It is convention (although not required by Joomla core) to name and position them, as shown here in the template named Element.
/element/template_thumbnail.png /element/params.ini /element/css/template.css /element/images/logo.png
These are just examples. Some common files in templates look like this
/element/template_thumbnail.png -- A web browser screenshot of the template (usually scaled down to about 140 pixels wide by 90 pixels high). Once the template is installed, it will act as a preview image, visible in the Joomla Admin Template Manager.
/element/params.ini -- A text file used to store the values of any parameters the template has.
/element/css/template.css -- CSS for the template. The folder location is optional, but you must specify its location in the index.php file. You can call it whatever you want. Typically, the displayed name will be used, but you'll find later that there are advantages to having other CSS files as well.
/element/images/logo.png – Any images that come with the template. Also for organizational reasons, most designers place them in image folders. Here we have an image file called logo.png as an example. The
templateDetails.xml
file acts as a manifest or packing list that includes a list of all files or folders that are part of the template. It also includes information such as author and copyright. Some of these details are displayed in the Template Manager's admin backend. An example of an XML file is shown here:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE install PUBLIC "-//Joomla! 1.6//DTD template 1.0//EN" "http://www.joomla.org/xml/dtd/1.6/template-install.dtd"> <install version="1.6" type="template"> <name>960TemplateTutorialStep1</name> <creationDate>1/10/10</creationDate> <author>Barrie North</author> <authorEmail>contact@compassdesigns.net</authorEmail> <authorUrl>http://www.compassdesigns.net</authorUrl> <copyright>Copyright (C) 2005 - 2010 Barrie North</copyright> <license>GPL</license> <version>1.6.0</version> <description>The first of 4 tutorial templates from Joomla 1.6 - A User's Guide</description> <files> <filename>index.php</filename> <filename>templateDetails.xml</filename> <filename>params.ini</filename> <folder>images</folder> <folder>css</folder> </files> <positions> <position>breadcrumbs</position> <position>left</position> <position>right</position> <position>top</position> <position>footer</position> <position>debug</position> </positions></p> <p> <config> <fields name="params"> <fieldset name="basic"> <field name="colorVariation" type="list" default="white" label="Color Variation" description="Base Color of template"> <option value="blue">blue</option> <option value="red">red</option> </field> </fieldset> </fields> </config> </install></p>
Let's see what some of the lines mean:
模板中使用的内容使用
<files> <filename>index.php</filename> <filename>templateDetails.xml</filename> <filename>params.ini</filename> <folder>images</folder> <folder>css</folder> </files>
“文件”部分包含所有通用文件,例如模板的 PHP 源文件或模板预览的缩略图。本节中列出的每个文件都由
index.php
内每个列出的职位生成内容的 PHP 代码精确匹配。index.phpp
index.php
文件中到底有什么?它是 HTML 和 PHP 的组合,决定了页面布局和呈现的所有内容。
让我们看一下实现有效模板的关键部分:index.php 文件顶部的 DOCTYPE
。这是每个网页顶部的一段代码。在我们页面的顶部,将其放入模板中:
<?php /** * @copyrightCopyright (C) 2005 - 2010 Barrie North. * @licenseGPL */ defined('_JEXEC') or die; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
第一个 PHP 语句仅显示版权/许可证,并确保文件不会被直接访问以确保安全。
网页 DOCTYPE 是浏览器如何显示网页的基本组成部分之一 - 如何处理各种 HTML 标记,更重要的是,浏览器如何解释 CSS。 A List Apart 的以下观察应该可以澄清一些事情:
您可以使用多个
DOCTYPE
。基本上,DOCTYPE
告诉浏览器使用什么版本的 HTML 来设计页面,是否有一些遗留代码或还包含 XML,以及如何解释页面。
这里开始出现“严格”和“过渡”一词(通常为 float:left
和 float:right
),以指示是否包含遗留代码。本质上,自从 Web 诞生以来,不同的浏览器对各种 HTML 标签和 CSS 版本都有不同程度的支持。例如,Internet Explorer 6 或更低版本将无法理解用于设置最小页面宽度的 min-width 命令。要复制效果以使其在所有浏览器中显示相同的效果,有时必须在 CSS 中使用特定于浏览器的“技巧”,以弥补每个浏览器遵守已发布标准的缺陷。
严格意味着 HTML 将完全按照标准的规定进行解释。过渡性 DOCTYPE
意味着该页面将允许与标准存在一些商定的差异(例如,继续使用已停止使用的标签)。
让事情变得复杂的是,有一种叫做“怪癖”模式的东西。如果 DOCTYPE
错误、过时或不存在,浏览器将进入怪异模式。这是向后兼容的尝试,因此 Internet Explorer 6 会像 Internet Explorer 4 一样呈现页面。
不幸的是,人们有时会意外地陷入怪癖模式。它通常以两种方式发生:
DOCTYPE
声明,链接最终为 DTD/xhtml1-strict.dtd,这是 WC3 服务器上的相对链接。您需要完整路径,如前面所示。DOCTYPE
之前而不是之后时,就会发生这种情况。接下来是一个 XML 语句(在 DOCTYPE
之后):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" >
我刚刚向您提供的有关 Internet Explorer 6 怪异模式的信息非常重要。在本教程中,您仅针对 Internet Explorer 6 及更高版本进行设计,并且需要确保它在标准模式下运行,以最大程度地减少您稍后必须执行的操作。
我们看一下index.php
文件头的结构;您希望它尽可能小,但仍然足以满足生产站点的需要。您将使用的标头信息如下:
<?php /** * @copyrightCopyright (C) 2005 - 2010 Barrie North. * @licenseGPL */ defined('_JEXEC') or die; $app = JFactory::getApplication(); ?> <!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 ?>/templates/system/css/system.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/template.css" type="text/css" /> </head>
这一切意味着什么?
我们已经讨论了 index.php
文件中 DOCTYPE
语句的含义。 <?php echo $this->语言; ? 代码从全局配置中的站点语言设置中提取语言。
$app = Jfactory::getApplication();
是一个变量,允许您获取各种参数(例如站点名称)并在模板中使用它们。下一行用于包含更多标头信息:<jdoc :include type="head"></jdoc>
此代码片段将在生成的页面(即您的前端)中插入全局配置中设置的所有标头信息。在默认安装中,它包括此处显示的标签:
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="robots" content="index, follow" /> <meta name="keywords" content="joomla, Joomla" /> <meta name="rights" content="" /> <meta name="language" content="en-GB" /> <meta name="description" content="Joomla! - the dynamic portal engine and content management system" /> <meta name="generator" content="Joomla! 1.6 - Open Source Content Management" /> <title>Home</title> <link href="/Joomla_1.6/index.php?format=feed&type=rss" rel="alternate" type="application/rss+xml" title="RSS 2.0" /> <link href="/Joomla_1.6/index.php?format=feed&type=atom" rel="alternate" type="application/atom+xml" title="Atom 1.0" />
大部分标题信息都是即时创建的,特定于某人正在查看的页面(文章)。它包括许多元标记和任何 RSS 源 URL。
标题中的最后几行提供了 Joomla 生成的页面的 CSS 文件链接,以及此模板中的 CSS 文件:
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" /> <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/template.css" type="text/css" />
前两个文件,system.css 和general.css,包含一些通用的 Joomla 样式。最后一项是模板的所有CSS,这里称为template.css。 PHP 代码 template ?> 返回当前模板的名称。以这种方式编写而不是编写实际路径会使代码更加通用。当您创建新模板时,您只需复制此行(以及整个标题代码)即可,而不必担心编辑任何内容。
模板 CSS 可以包含任意数量的文件,例如针对不同浏览器和不同媒体(例如打印)的条件文件。例如,以下代码检测并添加一个针对 Internet Explorer 6 怪癖的附加 CSS 文件(我们将在此处将其排除在工作示例之外):
<!--[if lte IE 6]> <link href="templates/<?php echo $this->template ?>/css/ieonly.css" rel="stylesheet" type="text/css" /> <![endif]-->
下一个示例是使用模板参数的技术的一部分。在本例中,在模板管理器中选择作为参数的颜色方案正在加载与所选颜色同名的 CSS 文件:
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/<?php echo $this->params- >get('colorVariation'); ?>.css" type="text/css" />
它可能会生成以下内容:
<link rel="stylesheet" href="/templates/960TemplateTutorialStep1/css/red.css" type="text/css" />
仍然在 index.php
文件中,现在页面的 部分已设置,我们可以继续处理 body 标记。创建您的第一个模板将会很容易!准备好了吗?
要创建模板,您所需要做的就是使用 Joomla 语句插入主体内容以及您想要的任何模块:
<body> <?php echo $app->getCfg('sitename');?><br /> <jdoc:include type="modules" name="top" /> <jdoc:include type="modules" name="left" /> <jdoc:include type="modules" name="breadcrumbs" /> <jdoc:include type="component" /> <jdoc:include type="modules" name="right" /> <jdoc:include type="modules" name="footer" /> <jdoc:include type="modules" name="debug" /> </body>
该模板包含以下内容,按合理的逻辑查看顺序排列:
此时(如果您预览它,请确保它是默认模板),该网站看起来不太令人惊叹。
您希望尽可能接近语义标记。从网络的角度来看,这意味着任何人都可以阅读页面——浏览器、蜘蛛或屏幕阅读器。语义布局是可访问性的基石。
请注意,您使用了 Joomla 特有的多个命令中的第一个来创建此输出:
<body> <?php echo $app->getCfg('sitename');?><br /> <jdoc:include type="modules" name="top" /> <jdoc:include type="modules" name="left" /> <jdoc:include type="modules" name="breadcrumbs" /> <jdoc:include type="component" /> <jdoc:include type="modules" name="right" /> <jdoc:include type="modules" name="footer" /> <jdoc:include type="modules" name="debug" /> </body>
PHP echo 语句只是从 configuration.php
文件中输出一个字符串。在这里,您使用站点名称;您可以轻松地使用以下内容:
The name of this site is <?php echo $mainframe->getCfg('sitename');?><br /> The administrator email is <?php echo $mainframe->getCfg('mailfrom');?><br /> This template is in the <?php echo $this->template?> directory<br /> The URL is <?php echo JURI::base();?>
jdoc
语句插入来自模块或组件的各种类型的 HTML 输出。
该行插入组件的输出。它是什么组件将由链接的菜单项确定: <include type="component"></include>
此行插入模块位置的输出:<include type="modules" name="right"></include>
该行为所有位置设置为右侧的模块生成内容。为这些模块生成的内容按照模块管理器的顺序列中设置的顺序放置在页面中。这是完整的语法:
<jdoc:include type="modules" name="location" style="option" />
在本节中,您将使用纯 CSS 为 Joomla 模板制作三列布局。您还将使其成为“fixed
”布局。网页layouts主要有三种类型——fixed
、fluid
和jello
——它们都是指如何控制页面的宽度。
fixed
布局的宽度设置为某个 fixed
值。fluid
布局可以根据浏览器窗口增大和缩小。fluid
但介于一些最小值和最大值之间。几年前,fluid
宽度模板风靡一时。辅助功能人员喜欢它们,抓住浏览器窗口的一角并看到所有内容滑动真是太酷了。
但是现在,我不制作fluid
模板,而是专注于fixed
宽度模板。我坚信它们最适合当今的网络。四年前,很多人还在使用 800px
宽度的屏幕。 fluid
宽度的要点是,您可以拥有一个在 1024px
屏幕中看起来不错的网页,但仍然可以缩小到仍在使用的较小屏幕。
现在,屏幕的趋势却相反。人们正在使用巨大的屏幕; 32% 的浏览 Joomlahack.com 的人使用的分辨率超过
1024px
。
有了这些大屏幕和 960px
宽度布局,你会遇到一个新问题——可读性。研究表明,当您浏览 960px
时,屏幕上的可读性会下降。所以 fluid
的宽度会填满那个大屏幕,并且 a) 看起来很愚蠢,b) 会减慢你的阅读速度。
典型的设计可能使用表格来布局页面。表格作为一种快速解决方案非常有用,因为您只需将列的宽度设置为百分比即可。然而,表格也有一些缺点。例如,与 CSS 布局相比,表格有很多额外的代码。这会导致加载时间更长(冲浪者不喜欢)和搜索引擎性能较差。代码的大小大约可以加倍,不仅可以使用标记,还可以使用“间隔 GIF”,即放置在表格的每个单元格中的 1x1 透明图像,以防止单元格折叠。即使是大公司有时也会陷入桌子陷阱。
tr
和 td
)正在做什么。使用 CSS,只需检查几行。当谈到 CSS 布局时,出现了一种使用新框架的趋势。这个想法是使用一组一致的 CSS 来创建布局,然后维护该组以解决浏览器兼容性等各种问题。对于此模板,我们将采用 Nathan Smith 开发的 960 网格系统。它仍然不是很令人兴奋,但让我们看看不同部分的含义。
使用 960 网格系统,您只需用一个类指定您想要的网格有多大。在此示例中,我使用 12 列网格,因此要使标头横跨 960px
的整个宽度,请在 index.php 中使用:
<div id="header" class="container_12"></div>
对于我们的三列,我们在容器内添加网格,如下所示:
<div id="content" class="container_12"> <div id="" class="grid_3 "> <jdoc:include type="modules" name="left" /> </div> <div id="" class="grid_6"> <jdoc:include type="modules" name="breadcrumbs" /> <jdoc:include type="component" /> </div> <div id="" class="grid_3"> <jdoc:include type="modules" name="right" /> </div> </div>
请注意,具有 10px
列间距(通常称为装订线)的内容已经有了一些喘息空间。这一切都是由聪明的 960 CSS 网格框架自动完成的,并且所有浏览器问题(是的,我们指的是您,Internet Explorer)都得到了处理。
index.php
主要代码如下:
<body> <div id="header" class="container_12"> <?php echo $app->getCfg('sitename');?><br /> <jdoc:include type="modules" name="top" /> </div> <div id="content" class="container_12"> <div id="sidebar" class="grid_3 "> <jdoc:include type="modules" name="left" /> </div> <div id="maincolumn" class="grid_6"> <jdoc:include type="modules" name="breadcrumbs" / <jdoc:include type="component" /> </div> <div id="sidebar-2" class="grid_3"> <jdoc:include type="modules" name="right" /> </div> </div> <div id="footer" class="container_12"> <jdoc:include type="modules" name="footer" /> </div> <jdoc:include type="modules" name="debug" /> </body>
在此示例中,我将 CSS 文件重命名为 layout.css
。使用 960 网格框架,我们很少需要接触这个文件,并且可以尽可能地压缩它。 layout.css
文件的关键部分如下所示:
.container_12 { margin-left:auto; margin-right:auto; width:960px; } .alpha { margin-left:0 !important; } .omega { margin-right:0 !important; } .grid_1,.grid_2,.grid_3,.grid_4,.grid_5,.grid_6,.grid_7,.grid_8,.grid_9, .grid_10,.grid_11,.grid_12,.grid_12 {display:inline; float:left; position:relative; margin-left:10px; margin-right:10px; } .container_12 .grid_1 { width:60px; } .container_12 .grid_2 { width:140px; } .container_12 .grid_3 { width:220px; } .container_12 .grid_4 { width:300px; } .container_12 .grid_5 { width:380px; } .container_12 .grid_6 { width:460px; } .container_12 .grid_7 { width:540px; } .container_12 .grid_8 { width:620px; } .container_12 .grid_9 { width:700px; } .container_12 .grid_10 { width:780px; } .container_12 .grid_11 { width:860px; } .container_12 .grid_12 { width:940px; }
很简单,所有内容都向左浮动,并且各种网格大小根据其所需的宽度设置。它是一个 12 列的网格,因此,例如 grid_6 表示六列,即 460 像素 - 全宽度减去填充。这个简单的布局非常适合学习如何在 Joomla 中使用 CSS,因为它显示了 CSS 相对于基于表格的布局的两个优点:代码更少,并且更易于维护。
按源排序的布局对于 SEO 效果更好。
但是,这个简单的布局是按照您在屏幕上看到内容的顺序在代码中排序的。将最重要的内容放置在生成的 HTML 源代码的开头,但在屏幕上仍具有相同的按查看器排序的外观,即左侧列显示在中心列之前(即左侧),这不是“源排序” .
按源代码排序的布局比重要内容出现在代码后期的布局更适合 SEO。从 Joomla 网站的角度来看,重要的内容是来自主体组件的内容。目前,为了保持 CSS 简单,我们将坚持使用这种按查看器排序的布局,并在本文后面更改为按源排序的布局。许多商业模板(例如 Joomlahack 的模板)进一步发展了这种按源排序的概念。
到目前为止,所有 CSS 都只是关于布局,这使得页面变得简单。因此,让我们添加一些格式,将 CSS 放置在名为typography.css 的新文件中。记得将其添加到index.php文件中!
当您开始使用 CSS 进行排版时,您应该设置一些整体样式并包括一个简单的全局重置:
/*Compass Design typography css */ * { margin:0; padding:0; } h1,h2,h3,h4,h5,h6,p,blockquote,form,label,ul,ol,dl,fieldset,address { margin: 0.5em 0; } li,dd { margin-left:1em; } fieldset { padding:.5em; } body { font-size:76%; font-family:Verdana, Arial, Helvetica, sans-serif; line-height:1.3; }
全局重置的目的是覆盖每个浏览器中不同的默认设置,并获得一个干净、一致的起点,无论页面显示在哪个浏览器上。
所有内容都被赋予零边距和填充,然后所有块级元素都被赋予底部和底部边距。这有助于实现浏览器的一致性。 (上面的第一个 CSS 选择器称为星形选择器,即使在 Internet Explorer 6 中,它也可以充当通用选择器。)您可以在此处和此处阅读有关全局重置的更多信息。
您将字体大小设置为 76%
以尝试在各个浏览器中获得更一致的字体大小。然后在 ems
中设置所有字体大小。设置 line-height:1.3 有助于提高可读性。当您在 ems
中设置字体和行高时,页面将更易于访问,因为查看者将能够根据自己的喜好调整字体大小,并且页面将重排并保持可读。此处将对此进行进一步讨论。
如果您要向标题、侧边栏和内容容器添加一些背景颜色,您会看到如下所示的内容。
请注意,侧栏未到达页脚。这是因为它们仅延伸到其内容;如果左侧和右侧的空间为白色,则侧列不存在。
如果您的模板的所有三列都是白色背景,那么这没有问题。您将使用这种方法,并且模块周围会有方框。如果您想要彩色或带有方框的等高列,则必须使用某种技术使列具有相同的高度。一种常见的解决方案是使用 JavaScript 脚本来动态计算和设置高度。
当在 index.php
文件中调用模块时,有多种显示方式的选项。语法如下:
<jdoc:include type="modules" name="location" style="option" />
样式是可选的,在 templates/system/html/modules.php
中定义。目前,默认的 modules.php
文件包含以下布局选项:table、horz、xhtml、rounded 和 none。让我们简要了解一下每个选项所需的代码行:
OPTION="table"
(默认显示)模块显示在一列中。如果我们使用 "table"
选项,下面显示了 Joomla 的输出。请注意,PHP 语句将被实际内容替换:
<table cellpadding="0" cellspacing="0" class="moduletable<?php echo $params- >get('moduleclass_sfx'); ?>"> <?php if ($module->showtitle != 0) : ?> <tr> <th valign="top"> <?php echo $module->title; ?> </th> </tr> <?php endif; ?> <tr> <td> <?php echo $module->content; ?> </td> </tr> </table>
OPTION="horz"
使模块水平显示。每个模块都在包装器表的单元格中输出。如果我们使用 "horz"
选项,以下显示了 Joomla 的输出:
<table cellspacing="1" cellpadding="0" border="0" width="100%"> <tr> <td valign="top"> <?php modChrome_table($module, $params, $attribs); ?> </td> </tr> </table>
OPTION="xhtml"
使模块显示为简单的 div 元素,标题位于 H3 中
标签。如果我们使用 "xhtml"
选项,以下显示了 Joomla 的输出:
<div class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>"> <div class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>"> <?php if ($module->showtitle != 0) : ?> <h3><?php echo $module->title; ?></h3> <?php endif; ?> <?php echo $module->content; ?> </div>
OPTION="rounded"
使模块以允许可拉伸圆角等的格式显示。如果使用 $style
,则 div 名称从 moduletable
更改为 module
。如果我们使用 "rounded"
选项,以下显示了 Joomla 的输出:
<div class="module<?php echo $params->get('moduleclass_sfx'); ?>"> <div> <div> <div> <?php if ($module->showtitle != 0) : ?> <h3><?php echo $module->title; ?></h3> <?php endif; ?> <?php echo $module->content; ?> </div> </div> </div> </div>
OPTION="none"
使模块显示为不包含任何元素和标题的原始输出。
如您所见,CSS 选项(xhtml
和 rounded
)的代码更加简洁,这使得设计网页样式变得更加容易。除非绝对需要,否则我不建议使用选项(后缀)表(默认)或 horz。
如果您检查前面显示的 modules.php
文件,您将看到模块中存在的所有这些选项。添加您自己的内容很容易;这是 Joomla 1.6 新模板功能的一部分。
要开发模板,您可以将模块样式xhtml
放在index.php
中的所有模块上:
<body> <div id="header" class="container_12"> <?php echo $app->getCfg('sitename');?><br /> <jdoc:include type="modules" name="top" style="xhtml" /> </div> <div class="clear"></div> <div id="content" class="container_12"> <div id="sidebar" class="grid_3 "> <jdoc:include type="modules" name="left"style="xhtml"/> </div> <div id="maincolumn" class="grid_6"> <jdoc:include type="modules" name="breadcrumbs" style="xhtml" <jdoc:include type="component" /> </div> <div id="sidebar-2" class="grid_3"> <jdoc:include type="modules" name="right" style="xhtml" /> </div> </div> <div class="clear"></div> <div id="footer" class="container_12"> <jdoc:include type="modules" name="footer" style="xhtml" /> </div> <jdoc:include type="modules" name="debug" /> </body>
让我们从布局 div 中删除背景,并添加一些 CSS 来设置模块的样式,其中包含模块标题的边框和背景。
我们将以下内容添加到排版中。您的 CSS 文件现在应该如下所示:
#header{ font-size:2em; } #footer{ border-top: 1px solid #999; } a{ text-decoration:none; } a:hover{ text-decoration:underline; } h1,.componentheading{ font-size:1.7em; } h2,.contentheading{ font-size:1.5em; } h3{ font-size:1.3em; } h4{ font-size:1.2em; } h5{ font-size:1.1em; } h6{ font-size:1em; font-weight:bold; } #footer,.small,.createdate,.modifydate,.mosimage_caption{ font:0.8em Arial,Helvetica,sans-serif; color:#999; } .moduletable{ margin-bottom:1em; padding:0 10px; /*padding for inside text*/ border:1px #CCC solid; } .moduletable h3{ background:#666; color:#fff; padding:0.25em 0; text-align:center; font-size:1.1em; margin:0 -10px 0.5em -10px; /*negative padding to pull h3 back out from .moduletable padding*/ ul.actions li{ float:right; list-style:none; border:0;} ul.actions li a img{ border:0;}
在这里,您为使用 style="xhtml" 生成的模块添加了特定的样式规则,因此每个模块都生成了 .moduletable
类的
您创建的排版 CSS 现在会产生如下所示的结果。
同样,使用 CSS 列表而不是表格可以减少代码并简化标记。将 CSS 用于菜单的其他优点之一是,各种 CSS 开发人员网站上都有大量示例代码。让我们看一下其中一个并看看如何使用它。
maxdesign.com 的网页有三十多个菜单可供选择,所有菜单都使用相同的底层代码。它被称为Listamatic。您必须更改代码才能使这些菜单适应 Joomla。
这些基于列表的菜单使用以下通用代码结构:
<div id="navcontainer"> <ul id="navlist"> <li id="active"><a href=" #" id="current">Item one</a></li> <li><a href="#">Item two</a></li> <li><a href="#">Item three</a></li><li><a href="#">Item four</a></li><li><a href="#">Item five</a></li></ul></div>
这意味着有一个名为 navcontainer
的封闭 <div>,并且 <code><ul> </ul>
有一个 id
of navlist
。要在 Joomla 中复制此效果,您需要某种封闭的
style="xhtml"
is 的模块的输出如下:<div class="moduletable"> <h3>...Module_Title...</h3> ...Module_Content... </div>
如果添加一个名为 menu
的模块后缀,它将被添加到 moduletable
类中,如下所示:
<div class="moduletablemenu"> <h3>...Module_Title...</h3> ...Module_Content... </div>
因此,从 Listamatic 中选择菜单时,您需要将 CSS 中的 navcontainer
类样式替换为 moduletablemenu
。
模块类后缀的使用很有用。只需简单更改模块类后缀即可使用不同颜色的框。
对于您的网站,假设您想要使用 Mark Newhouse 的列表 10(请参见此处)。你的 CSS 看起来像这样:
.moduletablemenu{ padding:0; color: #333; margin-bottom:1em; } .moduletablemenu h3 { background:#666; color:#fff; padding:0.25em 0; text-align:center; font-size:1.1em; margin:0; border-bottom:1px solid #fff; } .moduletablemenu ul{ list-style: none; margin: 0; padding: 0; } .moduletablemenu li{ border-bottom: 1px solid #ccc; margin: 0; } .moduletablemenu li a{ display: block; padding: 3px 5px 3px 0.5em; border-left: 10px solid #333; border-right: 10px solid #9D9D9D; background-color:#666; color: #fff; text-decoration: none; } html>body .moduletablemenu li a { width: auto; } .moduletablemenu li a:hover,a#active_menu:link,a#active_menu:visited{ border-left: 10px solid #1c64d1; border-right: 10px solid #5ba3e0; background-color: #2586d7; color: #fff; }
您需要将模块后缀菜单(在本例中没有下划线)添加到您想要使用这组 CSS 规则设置样式的菜单的任何模块中。这会生成如下所示的菜单
到目前为止,您的布局始终具有三列,无论这些列中是否有任何内容。从 CMS 模板的角度来看,这并不是很有用。在静态网站中,内容永远不会改变,但您希望网站管理员能够将内容放入任何列中,而不必担心编辑 CSS 布局。您希望能够自动关闭某列,或者在没有内容显示的情况下将其折叠。
Joomla 1.6 提供了一种简单的方法来计算为特定位置生成内容的模块数量,以便您可以添加对这些计数的一些 PHP 测试并隐藏任何空列或类似的未使用的 div 容器,并相应地调整布局。 PHP if 模块测试语法如下:
<?php if($this->countModules('condition')) : ?> do something <?php else : ?> do something else <?php endif; ?>
有四种可能的情况。例如,我们来统计一下图 9.7 中的模块数量。您可以将此代码插入到index.php中的某个位置:
left=<?php echo $this->countModules('left');?><br /> left and right=<?php echo $this->countModules('left and right');?><br /> left or right=<?php echo $this->countModules('left or right');?><br /> left + right=<?php echo $this->countModules('left + right');?>
因此,如果我们将此代码插入到模板中,我们可能会通过示例 Joomla 内容获得以下结果:
在这种情况下,您需要使用允许您对特定位置(例如右列)中存在的模块进行计数的函数。如果右栏没有发布内容,您可以调整栏大小来填充该空间。
有多种方法可以做到这一点。您可以将条件语句放在正文中以不显示内容,然后根据其中的列使用不同的内容样式。我们将利用网格系统,并根据一些计算简单地传递网格的大小。
在标头中,我们定义几个变量以确保它们具有默认值。
$leftcolgrid= "3"; $rightcolgrid= "3";
在模板的 HTML 中,我们可以使用这些变量来设置网格类:
<div id="content" class="container_12"> <div id="sidebar" class="grid_<?php echo $leftcolgrid;?>"> <jdoc:include type="modules" name="left"style="xhtml"/> </div> <div id="maincolumn" class="grid_<?php echo (12-$leftcolgrid-$rightcolgrid);?>"> <jdoc:include type="modules" name="breadcrumbs" style="xhtml" /> <jdoc:include type="component" /> </div> <div id="sidebar-2" class="grid_<?php echo $rightcolgrid;?>"> <jdoc:include type="modules" name="right" style="xhtml" /> </div> </div>
您会注意到我们正在回显 colgrid
值,然后进行简单的计算来查找主列,因为我们知道它们的总数必须为 12。
然后我们可以使用 countModules
函数来查找一些值。在我们的头脑中我们插入:
<?php if ($this->countModules('left') == 0):?> <?php $leftcolgrid = "0";?> <?php endif; ?> <?php if ($this->countModules('right') == 0):?> <?php $rightcolgrid = "0";?> <?php endif; ?>
请注意,我们正在检查左右位置是否有零个模块,因为我们已经将默认网格大小设置为 3。我们也可以通过真/假检查而不是数值来完成此检查(零)。
您已经完成了一半,但现在您已经扩展了中心列的宽度以容纳任何空的(即将隐藏)侧列。
创建可折叠列时,最好将模块设置为在没有内容时不生成。如果不这样做,页面中将包含空 div,这可能会导致跨浏览器问题。
为了不生成空 div,请使用以下 if 语句:
<?php if($this->countModules('left')) : ?> <div id="sidebar" class="grid_<?php echo $leftcolgrid;?>"> <jdoc:include type="modules" name="left"style="xhtml"/> </div> <?php endif; ?>
当您使用此代码时,如果left
位置没有发布任何内容,则
The above is the detailed content of Create a template with Joomla: step by step. For more information, please follow other related articles on the PHP Chinese website!