New Podcast

Posted Tuesday, January 31 at 3:28 pm
  Thomas Turnbull (tom_o_t on drupal.org) and Alan Palazzolo (zzolo on drupal.org) join Mike Anello to talk about their new book from O’Reilly Media, Mapping with Drupal. Mike’s usual co-hosts, Andrew and Ryan, were both unable to participate in the podcast, leaving Thomas and Alan subject to Mike’s long-winded (but extremely interesting by some accounts) questions.
Download Podcast 73
DrupalEasy_ep73_20120131.mp3
Syndicate content

NEWSLETTER

Stay informed on our latest news!

Syndicate content

Testimonial

Mike quickly spotted several flaws in our Drupal configuration and was able to teach us how to correct them. His training style and solid knowledge of Drupal makes for an efficient and productive training session. I came away with full confidence in the data I learned that day.

Who are we?

DrupalEasy is the collective expertise of Ryan Price and Michael Anello, who joined forces to provide training and consulting services worldwide. Read all about them and what they can do.

What is Drupal?

Drupal is a free, super-powerful content management system for sites that require information posting and collection, including blogs, forums, videos, photos, and databases of information. We think it is the best platform available. Here's why...

Why Drupal?

More and more savvy organizations are going with Drupal for content management, and its no mystery why. It’s free, flexible, and easy to maintain for small or large volume sites. Learn more...

Drupal Theming: $path vs. base_path and path_to_theme

Average: 3.5 (11 votes)

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;
}
?>

Trackback URL for this post:

http://drupaleasy.com/trackback/8

7 comments

Guest wrote 10 weeks 2 days ago

great helped lot

great helped lot

user picture
ryanprice wrote 36 weeks 1 hour ago

The 10th site would be the

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.

Guest wrote 37 weeks 6 days ago

What the heck you mean "In 9

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

user picture
ryanprice wrote 3 years 24 weeks ago

Thank you Dalin. The whole

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.

Dalin wrote 3 years 24 weeks ago

drupal_get_path() is not

drupal_get_path() is not portable from theme to theme so its not a good choice.

Ben wrote 3 years 24 weeks ago

It's easier to just use

It's easier to just use drupal_get_path() because it's much more flexible.

Pingback

[...] Go to the author’s original blog: Drupal Theming: $path vs. base_path and path_to_theme [...]

Add your comment

The content of this field is kept private and will not be shown publicly.
 
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options

Syndicate content