WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 9 months ago

#23470 closed defect (bug) (fixed)

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

Reported by: Caspie Owned by: 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 2 years ago.
23470bis.diff (643 bytes) - added by donutz 2 years ago.
23470.2.diff (1.1 KB) - added by obenland 15 months ago.
23470.3.diff (858 bytes) - added by obenland 10 months ago.
23470.4.diff (878 bytes) - added by obenland 10 months ago.

Download all attachments as: .zip

Change History (17)

@Caspie2 years ago

comment:1 @ocean902 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. :(

@donutz2 years ago

comment:2 follow-up: @donutz2 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.

comment:3 in reply to: ↑ 2 @nacin2 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.

comment:4 @tillkruess2 years ago

  • Cc me@… added

comment:5 @nacin18 months ago

  • Component changed from Template to Themes
  • Focuses template added

@obenland15 months ago

comment:6 @obenland15 months 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"].

@obenland10 months ago

comment:7 @obenland10 months 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.

comment:8 @ocean9010 months ago

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

@obenland10 months ago

comment:9 @obenland10 months ago

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

comment:10 @slackbot9 months ago

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

comment:11 @slackbot9 months ago

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

comment:12 @ocean909 months 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.