Training

Our next U.S. stop:

Mike Anello and Andrew Riley from Mediacurrent are teaming up to offer the first Blue Collar Git workshop on Friday, June 8 as part of DrupalCamp Charlotte (also part of the Southeast LinuxFest). The cost is only $149 for the full day if you register during the month of May ($199 otherwise). 

New Podcast

Posted Wednesday, May 9 at 7:50 am
Brandon Morrison (Brandonian on drupal.org) joins Andrew Riley and Mike Anello on the first post-DrupalCon Denver edition of the podcast to talk about all things Geo in Drupal 7. Brandon is one of the maintainers of the GeoField module and is an active member of the Drupal Geo community.
Download Podcast 82
DrupalEasy_ep82_20120509.mp3
Syndicate content

Newsletter

Stay up-to-date on DrupalEasy workshops and other events. On average, we send about one newsletter per month.

Testimonial

Mike is a great teacher!

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.7 (13 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

8 comments

Pere wrote 14 weeks 12 hours ago

thanks, it was that i'm

thanks, it was that i'm finding.

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

is good solucion for me.

good morning

Guest wrote 24 weeks 3 days ago

great helped lot

great helped lot

ryanprice wrote 50 weeks 22 hours 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 51 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?

ryanprice wrote 3 years 38 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 38 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 38 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