Archive for November, 2009

Load jQuery on Any Page

Saturday, November 28th, 2009

I found this little snippet of code, simply stick it in your address bar and it will create a script element that loads jQuery from Google, so you can use jQuery calls from console.

javascript:void((function(){j=document.createElement("SCRIPT");j.src="http://code.jquery.com/jquery-latest.pack.js";document.getElementsByTagName("HEAD")[0].appendChild(j);})())

Tags:
Posted in Code | 2 Comments »

Name Your Drupal Template Files By Node Title

Monday, November 9th, 2009

Out of the box, Drupal has a really awesome cascading filename theming system. It is really easy to theme idividual content types simply by naming your file “page-contenttype.tpl.php”. But many times, I want to be able to create a bunch of “Page” content types really quickly (ex. “About Us”, “Privacy Policy”, etc) and theme it uniquely by node. Personally, I think it’s easier to be able to name your files according to the title of the node, and not by IDs, because when I come back to a project in a month, I won’t remember what “14” is referring to.

To combat that, I use this little helper hook which I drop into template.php

function theme_preprocess_page(&$vars, $hook) {
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    switch($alias) {
      case "override":
        $vars['template_files'][] = "page-different-filename";
	  break;
      default:
        if($alias != $_GET['q']) {
	  $template_filename = 'page';
	  foreach (explode('/', $alias) as $path_part) {
	    $template_filename = $template_filename . '-' . $path_part;
	    $vars['template_files'][] = $template_filename;
	  }
        }
	break;
      }
   }
}

Now, by default, you can name your page template files by a hyphen-separated list phrase of the node title. So if I make a node called “About Us”, I can now theme the page by creating a template file named “page-about-us.tpl.php”. If you have a case where the default naming convention won’t work for you, simply put a case in the switch statement to case the particular page by slugged title.

Tags:
Posted in Code | 3 Comments »