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

Add hooks to indicate details of the template and template parts being rendered #32309

Open
bobbingwide opened this issue May 28, 2021 · 1 comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.

Comments

@bobbingwide
Copy link
Contributor

What problem does this address?

When developing a new theme it can be difficult to tell which template or template part is being rendered.
I added some debug information at the top of each of my templates to help identify the rendered template and template parts on the front end. eg

<!-- wp:html -->
<div class="WP_DEBUG">404.html</div>
<!-- /wp:html -->

If I wanted to check whether or not the template was being loaded from the file I would change the text in the div by adding a time stamp.

I subsequently attempted to develop a single block plugin ( https://github.com/bobbingwide/sb-debug-block ) that would semi-automate the information gathering. The block would not render anything when WP_DEBUG wasn't true.

In order to obtain the information I needed to display the debug information, I found I had to extend Gutenberg to add a couple of action hooks.
I propose that these action hooks become a standard part of Gutenberg/WordPress core.

These hooks will enable

  1. The level of debugging / problem determination that I want to achieve.
  2. The potential for server side rendering code to make decisions based upon the context.

What is your proposed solution?

Add two action hooks: rendering_template and rendering_template_part.

/lib/full-site-editing/template-loader.php

rendering_template will pass information about the selected template to the action hook.

function gutenberg_override_query_template( $template, $type, array $templates = array() ) {
        ...
        $block_template = gutenberg_resolve_template( $type, $templates );
     /**
     * Fires before the template is rendered.
     *
     * Allows extensions to follow template processing.
     * Called before the template is rendered.
     *
     * @param object $current_template The current template object.
     * @param string $type      Sanitized filename without extension.
     * @param array  $templates A list of template candidates, in descending order of priority.
     */
      do_action( 'rendering_template', $block_template, $type, $templates);

/packages/block-library/src/template-part/index.php

which becomes /build/block-library/blocks/template-part.php

Add two do_action hooks for rendering_template_part; one before the template part is rendered and the other after.
On the first call the attributes to the template part are passed.

 /**
     * Fires before/after the template part is rendered.
     *
     * Allows extensions to follow template part processing.
     * Called before and after each template part is rendered.
     *
     * @param array|null $attributes The template part attributes. Null after rendering.
     * @param array $seen_ids The current template part nesting.
     */
	do_action( 'rendering_template_part', $attributes, $seen_ids );

on the second call, the $attributes argument is null and the $seen_ids array no longer contains the template part that was rendered.

 /** This action is documented in template-part.php */
	do_action( 'rendering_template_part', null, $seen_ids );

This allows action hook function to track the template part nesting and un-nesting.

For more information on my prototyped solution see bobbingwide/sb-debug-block#2

bobbingwide added a commit to bobbingwide/gutenberg that referenced this issue May 28, 2021
@talldan talldan added [Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. labels Jun 9, 2021
@skorasaurus
Copy link
Member

skorasaurus commented Jul 9, 2021

Sharing my support for this concept in general although I'm personally agnostic of the means to accomplish this at this point.

A few different plugins like show-current-template and what-the-file displays which template is being used in the wp-admin bar for the currently displayed page in php-based/"classic" themes like shown below.

Selection_068

show-current-template, query-monitor, and what-the-file do not require WP_DEBUG to be enabled; I will use them on my production sites if I need to quickly check that a template is being properly loaded.

show-current-template relies on the php function get_included_files which is not applicable for FSE, block-based themes.

The popular query-monitor plugin also includes this template information as well cc (@johnbillion who may be interested in this discussion)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Technical Feedback Needs testing from a developer perspective. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants