By Jane


2012-08-15 13:45:06 8 Comments

What is the easiest way to select a template based on path alias. For example, if I have the following URLs:

http://www.example.com/mydirectory/page1
http://www.example.com/mydirectory/page2
http://www.example.com/mydirectory/page3

How would I go about creating a template for all of the pages in mydirectory? I should also add that using content type to theme these pages is not an option.

2 comments

@Colin Shipton 2014-03-25 10:38:30

It's actually easier than that using drupal_match_path() which is how the blocks module works out whether a block should be displayed on a page or not:

function MYTHEME_preprocess_page(&$vars) {
  if (drupal_match_path(drupal_get_path_alias(), 'mydirectory/*')) {
    $vars['theme_hook_suggestions'][] = 'page__mycustomtemplate';
  }
}

@Clive 2012-08-15 14:00:25

You can add some logic in a hook_preprocess_page() implementation that checks the alias, and adds theme hook suggestions if the path matches, e.g.

function MYTHEME_preprocess_page(&$vars) {
  $alias_parts = explode('/', drupal_get_path_alias());

  if (count($alias_parts) && $alias_parts[0] == 'mydirectory') {
    $vars['theme_hook_suggestions'][] = 'page__mycustomtemplate';
  }
}

Then if you create a page--mycustomtemplate.tpl.php file in your theme, and clear the cache, any pages with an alias beginning mydirectory/ will use that new file instead of the standard page.tpl.php.

Related Questions

Sponsored Content

0 Answered Questions

How do I create a path alias and redirect node?

2 Answered Questions

[SOLVED] Correct image path to use in template files of custom themes

  • 2011-10-15 15:02:27
  • Shafiul
  • 20476 View
  • 7 Score
  • 2 Answer
  • Tags:   7 theming

1 Answered Questions

[SOLVED] enforce naming conventions for page aliases by content type

  • 2016-09-08 06:27:29
  • tanbog
  • 25 View
  • 0 Score
  • 1 Answer
  • Tags:   8 path-aliases

3 Answered Questions

[SOLVED] Multiple path alias for a url

  • 2015-09-29 05:22:21
  • Guru
  • 1355 View
  • 2 Score
  • 3 Answer
  • Tags:   7 path-aliases

1 Answered Questions

[SOLVED] Pathauto URL alias not working (using nginx)

0 Answered Questions

URL alias not working for costume content type page.tpl file

  • 2015-05-30 08:20:58
  • Wiram Rathod
  • 75 View
  • 0 Score
  • 0 Answer
  • Tags:   7 path-aliases

1 Answered Questions

[SOLVED] Theming Subpages

  • 2013-08-13 18:07:48
  • Lars Townsend
  • 442 View
  • 0 Score
  • 1 Answer
  • Tags:   theming nodes 7

4 Answered Questions

[SOLVED] How to set one path alias as the canonical alias?

  • 2013-06-04 02:46:01
  • Kenny Wyland
  • 1467 View
  • 4 Score
  • 4 Answer
  • Tags:   7 path-aliases

2 Answered Questions

3 Answered Questions

[SOLVED] Template file naming with alias

  • 2012-12-06 15:21:56
  • Dan
  • 715 View
  • 0 Score
  • 3 Answer
  • Tags:   theming

Sponsored Content