Archive for April, 2007

Web hosting resellers - CHAPTER 5 ADDING AND CUSTOMIZING THEMES 165

Monday, April 30th, 2007

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 165 Node.tpl.php Any time a node (blog, page, event, image, and so on) is rendered, the node.tpl.php template is used. This template handles nodes that are displayed in full and nodes that are displayed as a teaser, such as they would appear in lists of nodes. You can also make a separate template for specific node types, as you ll see after the description of the default node template. Table 5-7 lists the variables available in node.tpl.php. Table 5-7. Variables Available in node.tpl.php Variable Description $content Contains the node s teaser or the node s body, depending on the page context. This variable exists so that the logic of determining whether to show the full body or only the teaser can be left out of the template. The $nodevariable still has the $node->teaser and $node->body available, however. So, if you wish to use different logic for determining which to show, you can still do so. $date The formatted date of this node s creation. $links The HTML containing the node s contextual links, such as links to the comments or attachments. $teaser A Boolean that is true if the node is appearing in a context, like the front page, where only the teaser should be shown. $name The node author s username with a link to her profile page. $node The node object itself. This is very helpful to have available in the template, as modules can add their own fields to this object. Use this object to customize the node s theme based on these added fields. The following $node-> variables are the standard fields of the $node object. $node->body The entire node contents. $node->changed The last modification date, as a UNIX timestamp. $node->created The creation date, as a UNIX timestamp. $node->nid The ID of the node. $node->teaser A shortened version of the node body. $node->title The title of the node. $node->type The content type (story, blog, forum, and so on). $node->uid The ID of the author. $node->username The username of the author. $node_url The node s permalink as a Drupal path. $picture The HTML to output the user s user picture. $submitted Information on who submitted the node and when: Submitted by admin on Tue, 08/02/2005 - 18:29. $terms HTML code containing links to the taxonomy terms for this node. $title The node s title as a string. $sticky A Boolean that is true if the sticky flag is set (meaning it should stay at the top of lists). $taxonomy An array of links to the pages for the taxonomy terms associated with this node. Continued
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

164 CHAPTER 5 (Make my own web site) ADDING AND CUSTOMIZING THEMES

Monday, April 30th, 2007

164 CHAPTER 5 ADDING AND CUSTOMIZING THEMES Table 5-6. Continued Variable Description $content The comment itself. $date The comment s formatted submission date (same as date in $submitted). $links The contextual control links, like reply, edit, and delete, as a single string. $new If the user has not yet viewed this comment, $new will contain the translated text new. Otherwise, it will be an empty string (use $comment->new to test if the comment is new). $picture If enabled and available, the comment author s picture in a hyperlink to the user s profile page. $submitted The translated text Submitted by user_linkon date. $title The subject of the title as a hyperlink to the comment itself using an anchor (#). This is like a permalink to the comment. $id An integer that uniquely identifies this comment among all comments on the page. $zebra Alternates between the strings even and odd, in case you want to use alternating styles. $is_front A Boolean that will be true if the page being generated is the site s front page. The default comment.tpl.php template, shown in Listing 5-3, can be found at themes/engines/phptemplate/comment.tpl.php. Listing 5-3. Default comment.tpl.php





Notice that $comment->new plays a big role in templating the comments. If a user has not yet read a comment, the enclosing

gets an extra class comment-new, and an anchor with the ID newis included in the output, as well as the translated text new. If the user s picture is printed, the line
will also be printed at the end of the comment. This draws on the class clear, which is defined in the default Drupal style sheet (misc/drupal.css) and prevents floating images from breaking the layout. The drupal.cssstyle Themes section.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision
coldfusion web hosting services

CHAPTER 5 ADDING AND CUSTOMIZING THEMES (Space web hosting) 163

Sunday, April 29th, 2007

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 163 Table 5-5. Variables Available in box.tpl.php Variable Description $content The box s content. $region Specifies the region on the screen in which the box appears. Can be main, left, or right. Defaults to main. $title The title of the box. $id An integer that uniquely identifies this block among all blocks on the page. $zebra Alternates between the strings evenand odd, in case you want to use alternating styles. $is_front A Boolean that will be true if the page being generated is the site s front page. The default box.tpl.php template, shown in Listing 5-2, can be found at themes/engines/ phptemplate/box.tpl.php. Listing 5-2. Default box.tpl.php

Comment.tpl.php The comment.tpl.php file overrides the theme_comment function for the output of comments to posts, including forums. Table 5-6 lists the variables available in comment.tpl.php. Note The theme_comment themable function is not responsible for controlling the threading of comments. See theme_comment_thread_min() and theme_comment_thread_max() in modules/ comment.module for the functions responsible for threading. Table 5-6. Variables Available in comment.tpl.php Variable Description $author Name of the comment s author as a link to his profile page. $comment The comment object as passed to the theme_commentfunction. The following $comment->variables are the standard fields in the $commentobject. $comment->subject The comment s subject. $comment->comment The content of the comment (same as $content). $comment->name The name of the user who submitted the comment. $comment->timestamp The UNIX timestamp from when the comment was created. $comment->uid The user ID from the user who added the comment. $comment->new A Boolean value that is equal to 1 if the comment is being viewed for the first time by the current user, and 0 if the user has seen it before. This is used to add the red new text to new comments on Drupal.org. Continued
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Space web hosting - 162 CHAPTER 5 ADDING AND CUSTOMIZING THEMES

Sunday, April 29th, 2007

162 CHAPTER 5 ADDING AND CUSTOMIZING THEMES Table 5-4. Variables Available in block.tpl.php Variable Description $block The block object (see http://php.net/oop). The following $block-> variables are the standard fields of the $block object. $block->content The content of the block. $block->module The name of the module that is responsible for creating this block. $block->region Can be any of the regions defined in the phptemplate_regions() function: left, right, header, footer, content, as well as any custom regions that are defined in a theme_regions()function, if you have written one (see the Adding Custom Regions for Blocks section later in this chapter). $block->subject The title of the block. $block->delta A number given to the block by the module that produces it. $is_front A Boolean that will be true if the page being generated is the site s front page. $id An integer that uniquely identifies this block among all blocks on the page. $block_id An integer that uniquely identifies this block among all blocks of the same region. $zebra Alternates between the strings even and odd, in case you want to use alternating styles. $block_zebra Like $zebra, but is reset for each block region. The default block.tpl.php template, shown in Listing 5-1, can be found at themes/engines/phptemplate/block.tpl.php. Listing 5-1. Default block.tpl.php

module” ?>” id=”module-$block->delta”; ?>”>

subject ?>

content ?>

As you can see, the two visual elements being used are the block s title, $block->subject, and the block s content, $block->content. The rest is the HTML that goes around the title and content. Notice that the outer

tag has two class attributes: block and block-module (modulewill be the name of the module that produced the block). This allows the style sheet to address the block generically as well as specifically according to the originating module. Box.tpl.php The box.tpl.php file overrides the theme_box function to enclose content in a generic box, usually a
tag, along with a title. The box.tpl.php template overrides the standard theme_box function, found in includes/theme.inc. Table 5-5 lists the variables available in box.tpl.php.
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 161 (Web site design and hosting)

Saturday, April 28th, 2007

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 161 The PHP code in template files rarely gets more complicated than this, and most of the time it will suffice if you can recognize which lines and bits belong together, so that you can copy and paste them without introducing errors. Tip To learn more about PHP code, read Beginning PHP5 and MySQL: From Novice to Professional,by W. Jason Gilmore (Apress, 2004). Template Variables Template files are the meeting place of static content (HTML) and dynamic values (variables), as well as minimal logic, similar to the examples shown in the previous section, to account for the numerous variations that are possible in a dynamic site. While there are many approaches and philosophies for templating in general, PHPTemplate has evolved in a way that encourages simple and minimal logic. It uses variables that are pushed into the template from the theme engine. Each template will have a number of variables available to it that are provided by the theme engine. In addition to the variables that are unique to each template, the variables $id and $zebra are always included by PHPTemplate, no matter which template is being called. These variables work as follows: $id: A sequential counter that is incremented every time the template is rendered. In the case of blocks, for example, it allows each block to have a unique ID on the page. $zebra: An alternating counter that has the value oddor even. This is useful for giving table rows alternating colors, for example. Furthermore, the variable $is_front is also available. It is true whenever the front page is being displayed, so you can always write template code like this: — display when on front page – PHPTemplate Template Files Here is an in depth look at the four tpl.php files included with PHPTemplate, as well as the page.tpl.php file that is included with the Bluemarine theme. Block.tpl.php The block.tpl.php file is included with the PHPTemplate engine and is therefore optional within individual themes. It is responsible for calls to theme(’block’) and generates the individual blocks within the various block regions. These are the blocks that you configure from the block administration page (admin/blocks). Table 5-4 lists the variables available in block.tpl.php.
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Web server logs - 160 CHAPTER 5 ADDING AND CUSTOMIZING THEMES

Saturday, April 28th, 2007

160 CHAPTER 5 ADDING AND CUSTOMIZING THEMES HTML code without needing to worry about escaping quotation marks or concatenating strings a fact that greatly improves the readability of template files over PHP functions that do the same thing. PHP Code and Templates If you are a web designer and you ve been given the task of creating a Drupal theme, you need not be scared of the fact that the templates mix PHP and HTML code. Even if you ve never programmed PHP before, the amount of skill and knowledge needed to work with the themes is small enough that you will be able to learn it quickly. Here are some examples of what you might see and explanations of what they do. The first is a simple printconstruct: This construct prints the contents of a variable (sends the contents to the browser for display on the screen). In this case, the variable $picture is being printed. The tags identify the parts that are to be interpreted as PHP and keep the PHP separate from the rest of the template. Here is a conditional print construct: This is a more elaborate version of the first example, which uses the ? and : to make a decision. In this case, it can be read if $sticky is set, print the text sticky; otherwise, print nothing (”"). The following uses if to make a decision:
In this example, the first line asks is $picture true? If so, the text
will be printed. Note that the opening if statement must be closed by a corresponding endif statement. Finally, this code creates a list:

This example shows how to build an unordered list

    with a variable number of list items
  • . It takes the variable $primary_links, which is an array, and uses the foreach operator to go over the array one item at a time, pointing the variable $link to the current item. For each primary link, an
  • element with the link s contents is printed. Notice the endforeach tag, which is needed to close the foreach tag.
    Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

CHAPTER 5 (Anonymous web server) ADDING AND CUSTOMIZING THEMES 159

Friday, April 27th, 2007

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 159 Exercise 5-1. Theme Breadcrumb Links Try your hand at overriding a themable function for the Bluemarine theme. Create a new file on your web server in the folder themes/bluemarine called template.php. Copy the following short code segment into the template.php file. Don t forget the .

Hello World!

‘; $output .= implode(’ / ‘, $breadcrumb); return $output; } ?> In the unaltered Bluemarine theme, breadcrumb links are separated using the character. This is the default HTML that is generated by Drupal. By including the phptemplate_breadcrumb function in the Bluemarine theme, the default HTML will no longer be used; instead, the HTML generated by the new function will be used. The phptemplate_breadcrumb function not only separates the breadcrumb links with a different character, but it also outputs a meaningful and original message directly above them. Click through some pages on your site and you will see the result. You have successfully altered the default HTML for breadcrumbs from this:

to this:

Hello World!

Home / create content Using Template Files In Exercise 5-1, you saw how a function could be written to override a themable function. As useful as this is, generating HTML from within functions can be very tiresome work. PHPTemplate makes the process of creating themable functions much easier and modular by introducing template files. The required file page.tpl.php is one such template file that will be used whenever theme(’page’) is called. This template must be provided by the theme itself. Other template files are provided by the PHPTemplate engine. These include block.tpl.php, box.tpl.php, comment.tpl.php, and node.tpl.php (see Figure 5-5 earlier in the chapter). These files override the block, box, comment, and node themable functions, respectively. If any of these four files appear in your theme folder alongside page.tpl.php, they will override the version in themes/engines/phptemplate. The template files are completely reusable; you could trade and mix the five standard tpl.php files between themes and expect, at a minimum, that they will be called at the appropriate time. Furthermore, since they are include files and not functions, you can write all the
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

158 CHAPTER 5 ADDING AND (Hosting web) CUSTOMIZING THEMES

Friday, April 27th, 2007

158 CHAPTER 5 ADDING AND CUSTOMIZING THEMES Theme engine s namespace: If the bluemarine namespace doesn t provide a function to theme foo, then Drupal looks to the theme engine namespace next. Assuming that Bluemarine is the active theme, the engine s namespace is phptemplate, as Bluemarine is a PHPTemplate theme. The function that Drupal will look for is therefore called phptemplate_foo($bar). Default namespace: Finally, if the function is not found in the engine s namespace, Drupal will use the default namespace, which is theme. A function named theme_foo($bar) will be called, and this is the function provided by the core include file or module. Thus, you can see that the default implementation of any themable function must begin with the prefix theme_. The active theme s own namespace is rarely used to override functions. You will not find many examples of bluemarine_foo, bluemarine_breadcrumb, and so on. Rather, you will see functions in the engine s namespace being written in the theme files. Anyone overriding a themable function is encouraged to take the engine s namespace to name the override functions. A strong and simple case can be made for this practice: if you want to move the function to a different theme or reuse it in some other way, you won t need to rename the function. Sticking with this policy will also allow you to copy whole theme directories and rename the directory, perhaps as the first step in creating a new theme. For the rest of the chapter, I will choose the option of naming themable functions after the theme engine and will not use the active theme s namespace. Note Pure themes (Chameleon) the themes that don t rely on an external theme engine are the exception to the advice of not using the active theme s namespace. If you need to override a theme function and you are using the Chameleon theme, you must use the Chameleon namespace. Table 5-3 shows some examples of common themable functions, what they would be named if overridden in the Bluemarine theme, and how they are evoked (parameters omitted). Table 5-3. Examples of Overriding Themable Functions in the Bluemarine Theme Default Version Bluemarine Version Invocation theme_links() phptemplate_links() theme(’links’) theme_submenu() phptemplate_submenu() theme(’submenu’) theme_xml_icon() phptemplate_xml_icon() theme(’xml_icon’) Exercise 5-1 demonstrates how to override a themable function.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Web hosting bandwidth - CHAPTER 5 ADDING AND CUSTOMIZING THEMES 157

Friday, April 27th, 2007

CHAPTER 5 ADDING AND CUSTOMIZING THEMES 157 theme_node($node, $teaser = FALSE, $page = FALSE): Provides the HTML wrapper for the content of a node. Based on the $teaser parameter, this function will return either the truncated preview version of a node or the full version. The $page variable determines whether the node is being displayed on its own page or in a list of nodes, and usually controls whether or not the title is printed as a heading. The function also handles calling theme_links() for any links that are to appear along with the node, such as taxonomy terms and whatever links have been added by modules, such as comment or subscription links. theme_comment($comment, $links = 0): Handles the output of comments. This includes comments made to posts like blogs as well as comments in a threaded forum. Note that this does not handle the threading itself, just the HTML around the variable $comment. Whatever links are associated with the comment are expressed in the variable $links. theme_block($block): Gets called for each block region and wraps the block content in HTML. theme_box($title, $content, $region = ‘main’): Builds a generic container around content, usually with the use of

tags. The theme_page($content) function is responsible for generating the actual page that gets sent to the browser. It is the last function that is called when building a page. To invoke this function correctly (without calling it directly), use the syntax theme(’page’, $content). This tells Drupal that a themable function called page is to be called with the parameter $content. Drupal then starts looking for the appropriate function. How Drupal Finds Themable Function Overrides When a Drupal programmer wants to generate output by calling a themable function, he uses a special invocation that follows the pattern theme($function_name, [$params, …])(see the theme() function in includes/theme.inc) to call the function dynamically instead of calling it directly. For example, to call the appropriate themable function theme_foo, you would use theme(’foo’, $params…). Tip The theme() function is used to call all theme_foo functions in Drupal. For example, to call theme_foo($param1, $param2), use the syntax theme(’foo’, $param1, $param2). Drupal then looks in several places to see if a matching function is implemented and calls the first one that is found. It checks three areas, or namespaces, to find overrides to themable functions: Theme s namespace: The first place Drupal checks is the active theme s namespace. If Bluemarine is the active theme, the namespace is bluemarine. Drupal will look for a function called bluemarine_foo() and call it with whatever extra parameters were passed to theme(). If theme(’foo’, $bar)is the original call, and a function named bluemarine_foo is found, the call will be bluemarine_foo($bar).
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Mac os x web server - 156 CHAPTER 5 ADDING AND CUSTOMIZING THEMES

Thursday, April 26th, 2007

156 CHAPTER 5 ADDING AND CUSTOMIZING THEMES and purpose, and also to override it by redirecting calls to an overriding function if one is available in the theme. Table 5-2 lists some themable functions and what they do. It is far from complete but it will give you an idea of how themable functions are used in Drupal. Notice that all of the func tions listed start with theme_. Table 5-2. Some Themable Functions Function Location Purpose theme_block includes/theme.inc Handles blocks theme_box includes/theme.inc Builds a container theme_breadcrumb includes/theme.inc Generates a breadcrumb trail theme_comment modules/comment.module Handles comments theme_form_element includes/theme.inc Styles elements that are typically found in forms, such as text boxes and input tags theme_links includes/theme.inc Takes a list of links such as primary and secondary links and styles them theme_book_navigation modules/book.module Is responsible for the previous, up, and next links in book hierarchies theme_page includes/theme.inc Generates Drupal pages theme_node includes/theme.inc Handles nodes theme_profile_listing modules/profile.module Takes one user and displays her profile fields (for use in lists of users) theme_submenu includes/theme.inc Generates a submenu theme_user_profile modules/user.module Generates the listing of a user s account information; the one seen by visiting user/1, for example theme_xml_icon includes/theme.inc Generates an XML icon Some of the themable functions listed in Table 5-2 are used often, as I ll explain next. Main Themable Functions A small number of themable functions are responsible for the overall layout and construction of a Drupal page, and they are most commonly implemented in custom themes. Here is a list of those functions and a short description of what they do: theme_page($content): Places $content in a complete HTML page. Typically, theme_page will call further themable functions to get additional output such as the blocks, status messages, and HTML headers. All custom themes implement their own version of theme_page. In PHPTemplate, this is manifested in the form of a page.tpl.php file, which is the only required file a PHPTemplate theme must have.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services