nov meeting review

Events happening in the community are now at Drupal community events on www.drupal.org.
sanguis's picture

Well it was small, 4 people but concidering the impromptu nature that was good.
I learned more about search hooks, and that all the freelancers in the area are overwhemed w/ bussiness.

JOsh

Comments

Theming

rick hood's picture

I handed out the following - which is everything I know about theming Drupal.

If I am missing something and/or there is a better way to do things, please let me know.


HTML and CSS

This is nothing to do directly with Drupal, but I would say I spend 80% of my theming time on that. I just wanted to mention that as CSS is really important and not so easy sometimes.


For he rest of this, the key concept is simply to understand that a Drupal page content is really just two things:

a. The HTML and CSS of the templates (page.tpl.php and node.tpl.php).

b. The variables that Drupal is printing within the HTML and CSS (e.g. $content) which contain the content from the database – and sometimes also contains its own HTML as part of the output (see breadcrumbs example below).

TPL (template) Files

You can create different page.tpl.php and node.tpl.php files for each content type.

If you want the story content type to have a different page layout from all others, then you simply copy page.tpl.php and rename it to page-story.tpl.php, then make your changes to that. Drupal automatically knows to use that TPL file for story node types.

If you just want to change the node layout and not the whole page layout, then you do the same with node.tpl.php --- > node-story.tpl.php.

Finding and printing content variables

Pulling out the variables you want to print so you can put them in a different place. By default, Drupal prints all node content by printing the variable $content. But you can pull out the variables that are within $content so you can re-arrange how where they go on he page.

So here is how to do that:

A. First, open your node.tpl.php file (in your theme folder) and put this at the bottom:

<pre>
<?php print_r ($node); ?>
</pre>

This will print all the variables that are available to you at the bottom of the page, so you can see what you need to grab.

(Another way to get that is to use the Drupal Devel Module.)

B. Then also in your node.tpl.php file, instead of doing this:

<?php print $content?>

You do this:

<?php print $node -> content['body']['#value']; ?>

In the above example, just the body of the node will print, and now you can take that and print it wherever you want to in the node (in node.tpl.php). You can do that with all other variables that you want to print.

NOTE: Just FYI $content in the node.tpl.php file is not the same as $content that you see in the page.tpl.php file. Yes, confusing.

I don't think you can pull apart the $content variable in the page – not sure – I think you can only do it in the node (in node.tpl.php).

Creating new regions to put blocks into.

You can create new regions in your page layout to be available as places to put blocks into by doing this the following: In the template.php file, find the code below and add your new region to the bottom as shown.

function zen_regions() {
  return array(
       'left' => t('left sidebar'),
       'right' => t('right sidebar'),
       'content_top' => t('content top'),
       'content_bottom' => t('content bottom'),
       'header' => t('header'),
       'footer' => t('footer'),
       'new_region' => t('new region')
  );
}

Then in the page.tpl.php page you might put this:

<?php if ($new_region):?>
<div id="new-region">
<?php print $new_region; ?>
</div>

The red part is the only mandatory thing, the rest is a suggestion – though the first line is recommended so it doesn’t print if it doesn’t exist.

Reformatting how variables are printed

If you need to re-format how a particular variable is output, you alter the theme function. The classic example is how to change the delimiter in a breadcrumb output, which is part of the $breadcrumb variable.

Here is the normal breadcrumb function:

function theme_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';
  }
}

How do I know that? One way you can find that is browse or search in api.drupal.org, for breadcrumb and you will find this: http://api.drupal.org/api/function/theme_breadcrumb/5

So, if I want to change the delimiter from this ">>" to this "|" I just do this:

A. Copy the above function into my template.php file (in the theme folder - if there is not such a file, create one, but most themes already have one).

B. Change 'theme' to the name of your theme (in this example, 'zen') and change '>>' to '|', as shown below.

function zen_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' | ', $breadcrumb) .'</div>';
  }
}

So now this will override the standard function and breadcrumbs will print out like this:

Home | Page | Another Page | etc...

Instead of this:

Home >> Page >> Another Page >> etc...

Which Version

skowyra's picture

Rick,

Which version of Drupal are you using? I'm in 5x and can't find the function to add a region. Is it a version issue?

Jim

Hi Jim

rick hood's picture

I'm in version 5, but in any version the code above goes into the template.php file of your theme:

sites/all/themes/zen/template.php (where zen is the name of the theme I was using the the example above).

So, its this code that goes there[except replace the word 'zen_regions()' with 'yourthemename_regions()']:

function zen_regions() {
return array(
'left' => t('left sidebar'),
'right' => t('right sidebar'),
'content_top' => t('content top'),
'content_bottom' => t('content bottom'),
'header' => t('header'),
'footer' => t('footer'),
'new_region' => t('new region')
);
}

If you don't have a template.php file, just create on and put the code in it.

Then the code to actually display the region is goes into your themes page.tpl.php file:

sites/all/themes/zen/page.tpl.php

<?php if ($new_region):?>
<div id="new-region">
<?php print $new_region; ?>
</div>

Got it

skowyra's picture

Rick,

A belated thanks for the update and assistance.

Jim

Western Massachusetts

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: