1

I am looking for some clarification on whether to use and underscore ("_") or a dash ("-") when creating a page template override for a content type. Some tutorials and articles say the format should use underscores such as ...

page--content_type_name.tpl.php

while others specify the format as dashes like ...

page--content-type-name.tpl.php

If my content type has a multi-word name how should the template override filename be formatted?

  • Both seem to work fine for me, have you tried them? – Clive Nov 21 '12 at 22:29
  • I have tried both and only the underscore works for me. – webworm Nov 21 '12 at 22:36
  • Hmm must be the theme I'm using or something but yeah, now you mention it the method with underscores has always worked – Clive Nov 21 '12 at 22:44
4

If you look at theme_get_suggestions(), there is a comment:

// When we discover templates in @see drupal_find_theme_templates,
// hyphens (-) are converted to underscores (_) before the theme hook
// is registered. We do this because the hyphens used for delimiters
// in hook suggestions cannot be used in the function names of the
// associated preprocess functions. Any page templates designed to be used
// on paths that contain a hyphen are also registered with these hyphens
// converted to underscores so here we must convert any hyphens in path
// arguments to underscores here before fetching theme hook suggestions
// to ensure the templates are appropriately recognized.

If you look at a preprocess, like template_preprocess_node(), you will see things like:

$variables['theme_hook_suggestions'][] = 'node__' . $node->type;

This is using the machine name of the content type directly, which is underscored.

-- is a delimiter between things, underscores are used within a thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.