Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Add support for dynamic templates #4659

Closed
wants to merge 1 commit into from

Conversation

aduth
Copy link
Member

@aduth aduth commented Jan 24, 2018

Related: #3588

Note: Experimental

This pull request seeks to explore dynamic templating, in an effort to support user-defined page layouts leveraging yet-to-be-implemented layout blocks (e.g. content, logo, menu, etc). The changes here introduce a new wp_template post type. When a page is loaded, we use the template hierarchy to attempt to find a valid dynamic layout post to use in place of the default theme template file. Priority matters here, as if a theme defines a post-143.php, it should still take precedence over a user-defined index template. Further, users may create templates which do not correspond to the template hierarchy at all, and which instead are associated with specific post types to be offered by the "Template" field in the editor (related: #991).

Templates Editor Front-End
templates editor front-end

Testing instructions:

Verify that creating a new Template post type with a slug corresponding to the template hierarchy is used in place of that theme template, when appropriate (no other files having precedence).

  1. Navigate to Templates > New Post
  2. Enter "Front Page" as the title, or an equivalent which would translate to a template hierarchy file (e.g. "Single")
  3. Enter content for the template
  4. Publish the post
  5. Navigate to a path of the site which should use the newly defined template, confirming that your content is shown in its place

Verify that creating a new Template post type and assigning its Post Types terms makes the template available as an option (in Classic Editor, see #991) for Page Attributes > Template.

  1. Navigate to Templates > New Post
  2. Enter a title
  3. In "Categories & Tags", assign a post type slug (e.g. post, page)
  4. Navigate to the post listing for the post type assigned in Step 3
  5. Publish the post
  6. Click "Classic Editor" under the "Add New" dropdown
  7. Confirm that your template is offered as an option under Page Attributes > Template

Next Steps:

  • Layout blocks (e.g. "Content", "Menu")
  • Post template field in Gutenberg
  • Strategy for non-visual layout elements (wp_head, wp_footer)

@aduth aduth added Framework Issues related to broader framework topics, especially as it relates to javascript Customization Issues related to Phase 2: Customization efforts labels Jan 24, 2018
Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. The flow of data (which hooks are influencing what; what the filtered data looks like) isn't immediately obvious, but I'd attribute that more to the nature of WordPress template hierarchy. The code is clear and points to the right places (@see tags).

lib/template.php Outdated
return $template_name;
}

// `$template` filter value is a absolute path, so check that template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an* absolute path

lib/template.php Outdated
// If post is explicitly assigned a dynamic template, return immediately.
if ( ! preg_match( '/\.php$/', $template_name ) ) {
return $template_name;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth extracting this check as a function? IMO that would be useful insofar as it codify what a dynamic template's slug looks like.

lib/template.php Outdated
*/
function gutenberg_map_template_slug( $template_name ) {
return preg_replace( '/\.php$/', '', $template_name );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map part of the name threw me off a bit. Is it a leftover of something using array_map, or is it map as in mapping a raw name to the proper one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was an attempt to clarify that the function is meant to be used in the context of array_map, but I guess this isn't really a clarification that needs to be made. Maybe "get" instead of "map" ?

lib/template.php Outdated
}
}

// Remove file extension from template paths before slug lookup.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spirit of our conversation moments ago about comments (😉), how 'bout something like "Prepare template paths for slug lookup"? That would preserve the motivation/intent and remove possible implementation details from the comment.

@aduth
Copy link
Member Author

aduth commented Jan 25, 2018

Related branch try/inject-template (diff) extends the behavior here to explore how we might inject the dynamic template within the theme's own intended output. This is made tricky by the fact that header.php and footer.php typically include visual elements, while we might assume a user-generated dynamic template would desire full control over the entirety of the visual layout. Alternatively, we could consider this a theme supports for new, Gutenberg-supporting themes.

Specifically, see d4b5f5e

@aduth
Copy link
Member Author

aduth commented Feb 13, 2018

Closing as experimental. Ideas will be split out into separate incremental steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Customization Issues related to Phase 2: Customization efforts Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants