Drupal Theming: $path vs. base_path and path_to_theme

When developing Drupal themes, there is one bit of code you type over and over again:

sites/all/themes/blueprint/images/whatever.jpg

Where images/whatever.jpg could be a css file, or other file, but is normally an image.

However, since clean URLs in Drupal appear to make fake directories, the web browser thinks your image is located in:

node/345/sites/all/themes/blueprint/images/whatever.jpg

When it's not. Luckily Drupal has tools to help you in this case.

base_path()

Normally, we'd solve this by adding a slash / to the beginning of the string, but Drupal is dynamic, and could live in any directory, not just the root, so we have the base_path() function. In most cases, this just throws a slash '/' back at you, but if your site is located at example.com/community, you should get '/community/'.

path_to_theme() and $directory

Also, you might copy code from one theme to another and you don't want to have to find and replace all the references to Blueprint theme. Therefore, Drupal provides the path_to_theme() function, and inside theme files, you also have a variable called $directory that holds this.

You could use:

<?php print '/'. $directory .'/' ?>images/whatever.jpg

In 9 out of 10 Drupal sites, one would use the following:

<?php print base_path() . path_to_theme() .'/' ?>images/whatever.jpg
<!--OR-->
<?php print base_path() . $directory .'/' ?>images/whatever.jpg

Blueprint to the Rescue

In Ted Serbinski's Blueprint theme, one can simply use:

<?php print $path ?>images/whatever.jpg

But if you don't use Bluepint, you have to create $path for yourself. Here's how:

In the _phptemplate_variables() function (Drupal 5) in the template.php file (if you don't have this function, add it):

<?php
function _phptemplate_variables($hook, $vars = array()) {
$path = base_path() . path_to_theme() .'/';
$vars['path'] = $path;
//...
return $vars;
}
?>

Comments

Thank you Dalin.

The whole point here is that Ted's $path variable works... everywhere inside themes, saves you typing, makes your paths flexible, allows you to rename your theme, no problem.

The Blueprint theme is like a masterclass in Drupal theming hacks - he's got lots of great stuff in there.

Submitted by admin on Fri, 08/22/2008 - 16:19

What the heck you mean "In 9 out of 10 Drupal sites"? Do I need a special prayer to manage that unpredictable code?

Submitted by Guest (not verified) on Thu, 05/19/2011 - 09:30

The 10th site would be the one implementing the extra variable, so you don't have to keep typing "path_to_theme" all day long.

Maybe by now (several years after this article was published) it is not such a high percentage any longer.

great helped lot

Submitted by Guest (not verified) on Sun, 11/27/2011 - 13:34

thanks, it was that i'm finding.

<?php print base_path() . path_to_theme() .'/' ?>

is good solucion for me.

good morning

Submitted by Pere (not verified) on Wed, 02/08/2012 - 18:39

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name