Make WordPress Core

Opened 11 years ago

Closed 9 years ago

#23470 closed defect (bug) (fixed)

Convert slash to dash and make body class for page templates look better

Reported by: caspie's profile Caspie Owned by: ocean90's profile ocean90
Milestone: 4.1 Priority: normal
Severity: normal Version: 3.4
Component: Themes Keywords: has-patch
Focuses: template Cc:

Description

If you keep you page templates in a page-templates folder in your theme. Let's we have a template named front-page.php - the template slug written in the postmeta will be page-templates/front-page.php

When generating the classes for the body, get_body_class() will convert the dot to a dash but the slash will be stripped by sanitize_html_class().
So finally we will have a class:

page-template-page-templatesfront-page-php

Isn't it prettier to convert the slash to dash and to have it like:

page-template-page-templates-front-page-php

Attachments (5)

23470.diff (613 bytes) - added by Caspie 11 years ago.
23470bis.diff (643 bytes) - added by donutz 11 years ago.
23470.2.diff (1.1 KB) - added by obenland 10 years ago.
23470.3.diff (858 bytes) - added by obenland 9 years ago.
23470.4.diff (878 bytes) - added by obenland 9 years ago.

Download all attachments as: .zip

Change History (17)

@Caspie
11 years ago

#1 @ocean90
11 years ago

  • Version changed from 3.5.1 to 3.4

Page templates in subdirs are supported since 3.4, see #11216.

Fixing it could break some themes. :(

@donutz
11 years ago

#2 follow-up: @donutz
11 years ago

  • Cc donutz added

I would like to make a case for fixing it.

First, something like page-template-page-templatesfront-page-php looks quite horrible, not to say buggy or erratic. I wouldn't use such a selector in my stylesheet :)

Second, moving templates to a sub-folder (and thus being forced to reapply them) breaks things as well (css), one expects to have access to the same body class selector, template located in sub-folder or not.

Third, it could be useful to have the sub-folder name as a separate body class, so one could group a bunch of templates and style them all in one go.

I attached a diff file with a possible solution.

#3 in reply to: ↑ 2 @nacin
11 years ago

Replying to donutz:

I would like to make a case for fixing it.

First, something like page-template-page-templatesfront-page-php looks quite horrible, not to say buggy or erratic. I wouldn't use such a selector in my stylesheet :)

Agree.

Second, moving templates to a sub-folder (and thus being forced to reapply them) breaks things as well (css), one expects to have access to the same body class selector, template located in sub-folder or not.

Disagree. When a template is moved to a subfolder, all posts using that page template break. I see no reason why the CSS class shouldn't also change.

Third, it could be useful to have the sub-folder name as a separate body class, so one could group a bunch of templates and style them all in one go.

Clever idea.

#4 @tillkruess
11 years ago

  • Cc me@… added

#5 @nacin
10 years ago

  • Component changed from Template to Themes
  • Focuses template added

@obenland
10 years ago

#6 @obenland
10 years ago

We won't be able to just change the way we pass on the page template class, too many themes are using the current syntax. We'd have to add the optimized version as an additional class.

I like the idea of having subfolder names as a separate body class, though it makes 23470.2.diff a bit more verbose than I'd like it to be. Using Twenty Fourteen as an example, theme authors could use attribute selectors to make that work too: [class*="page-template-page-templates"].

@obenland
9 years ago

#7 @obenland
9 years ago

  • Milestone changed from Awaiting Review to 4.1

For the initial example, the updated patch creates the following output:
page-template, page-template-page-templates, page-template-front-page-php, page-template-page-templatesfront-page-php.

This happens to address the concern of changing class names when moving page templates in and out of subfolders. To me it's just a more esthetic class name.

#8 @ocean90
9 years ago

Would be page-template-page-templates-front-page-phptoo much? Can we get rid of appending -php to the new ones?

@obenland
9 years ago

#9 @obenland
9 years ago

Updated patch, to avoid the php suffix in the new classes.
page-template-page-templates-front-page-php will stay around forever though.

This ticket was mentioned in Slack in #core by obenland. View the logs.


9 years ago

This ticket was mentioned in Slack in #core by obenland. View the logs.


9 years ago

#12 @ocean90
9 years ago

  • Owner set to ocean90
  • Resolution set to fixed
  • Status changed from new to closed

In 30100:

Improve body class for page templates.

  • Convert slash to dash too.
  • Add sub-folder name as a separate body class.
  • New classes are additional classes, old syntax will persists for BC.

With this /page-templates/full-width.php will produce page-template, page-template-page-templates, page-template-full-width and page-template-page-templatesfull-width-php.

props obenland, Caspie, donutz.
fixes #23470.

Note: See TracTickets for help on using tickets.