gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Allow usage of block based "template parts" without using block based "templates"

Open fabiankaegy opened this issue 4 years ago • 40 comments

What problem does this address?

Jumping all in on Block Based Themes can be quite difficult & disruptive. Especially on a legacy theme that has loads of customizations that currently are not feasible within a block based theme. There may be new areas of the site though or specific areas of the site where it would be interesting to use the new tools to build just a part of the theme in a block based manner.

Like for example a site that wants to build just the site footer using a block based template part.

What is your proposed solution?

Allow the usage of block based "template parts" without having to also allow block based "templates". In that case the "Editor" (previously Site Editor) would only display the "Template Parts" and not allow the creation of "Templates". And in the themes traditional footer.php file the theme author could render the block template part like so:

echo do_blocks( '
<!-- wp:template-part {"slug":"footer","theme":"fse","tagName":"footer","className":"site-footer","layout":{"inherit":true}} /-->
' );

In an ideal case theme authors would also be able to filter whether or not editors should be allowed to manually create their own "template parts" or whether only the ones provided by a theme are available.

Providing this template part first approach to adopting FSE would open the door to many more use cases that currently are not jet ready to jump all with their full content.

Note: This conversation was started in the #fse-outreach-experiment channel in the WordPress Slack

fabiankaegy avatar Jan 13 '22 10:01 fabiankaegy

@priethor just wanted to put this on your radar as this might be another great idea to consider for gradual adoption measures.

annezazu avatar Jan 13 '22 18:01 annezazu

This is precisely what the universal themes exploration was doing on the side. You'd end up with <?php echo gutenberg_block_template_part( 'header' ); ?> calls in your PHP theme.

It's something we could formalize into a core API (i.e. get_block_template_part( $slug )) but there's some decision that need to happen first around how coupled a template part is to a theme, which is up for revision.

mtias avatar Jan 17 '22 10:01 mtias

@mtias is there a ticket where the discussion around how coupled a template part is to a theme is being tracked? Would love to link it here to ensure we have a clear understanding of the dependencies.

fabiankaegy avatar Jan 17 '22 10:01 fabiankaegy

In WP 5.9 we already added 3 new functions to do these things: block_template_part, block_header_area and block_footer_area. You can see them in https://github.com/WordPress/wordpress-develop/blob/d085349cf5760294d1bdbc3f01e30a483d921f5d/src/wp-includes/block-template-utils.php#L872-L903 👍

aristath avatar Jan 17 '22 11:01 aristath

@aristath That is very cool! Thanks for sharing :)

So the main thing that this ticket is asking then is whether we can activate the site editor for template parts alone without needing to enable full on templates

fabiankaegy avatar Jan 17 '22 11:01 fabiankaegy

@fabiankaegy yes, correct, we are missing the ability to enter the site editor in the "focused template part" mode directly. We have the building pieces mostly in place, we just need to enable it for classic themes. It should be a good area of work for 6.0. A classic theme should not need to use full block templates to benefit from block template parts and a UI to manage them. The things to consider are mostly around the information architecture and access points.

mtias avatar Jan 25 '22 13:01 mtias

I agree that this would be an excellent stepping stone between classic themes and FSE and it's something my team is looking for.

In the meantime, we're using a custom post type as a stop-gap solution, which presents its own challenges.

graylaurenm avatar Mar 02 '22 18:03 graylaurenm

It seems we need some more documentation on how to use these from a PHP template.

mtias avatar Mar 25 '22 09:03 mtias

Fabian's proposed suite of features for a template-part-first adoption of Full Site Editing is the one thing I'm most excited about in WP's near future. I understand v6 is now in feature freeze, but I haven't yet seen any work towards this:

... activate the site editor for template parts alone without needing to enable full on templates

My current experience of adding an index.html file in /templates is an inoperable white screen for a site editor, and then the baffling realization that I have to give up any bespoke front-page.php in order for the site editor to even load successfully. The system is unusable to me as-is (or maybe I'm ignorant of how it wants to be used).

The block editor is amazing in many ways, but I can't get fully behind it when I feel it's removing things I enjoy having. I would love the ability to give content editors controlled "block editor windows" into their sites in the context of classic templating 👍

ALJ avatar Apr 22 '22 01:04 ALJ

The block editor is amazing in many ways, but I can't get fully behind it when I feel it's removing things I enjoy having. I would love the ability to give content editors controlled "block editor windows" into their sites in the context of classic templating +1

This exactly. It at least would make adopting FSE easier than an all-at-once or all-or-nothing approach. The Appearance > Editor screen would be great to have for just template parts and global styles, while still allowing templates themselves to be in PHP.

coreyworrell avatar May 19 '22 21:05 coreyworrell

This is a great idea that will help the move from classic themes to FSE. Been thinking how this can be implemented to support the intended use, and to discourage "overuse", deep-nesting, and similar.

Can easily see a classic theme that would use blocks in a hard-coded template part for the header, footer or sidebar. Thinking this would work and look great. That basically means the "widgets" in these areas are now native blocks with no restrictions!

On the other hand thinking that this should be limited to specific areas: header, footer, sidebar(s), main content. Nesting of hard-coded template parts may turn out to be pretty hard to support and maintain. Imagine having to support deep-nested blocks styles/CSS that may be changing from time to time, or scripts that are connected to or depend on specific blocks.

azaozz avatar May 25 '22 23:05 azaozz

Related: #41119

carlomanf avatar May 26 '22 01:05 carlomanf

The biggest challenge here is going to be how to give access to an interface for editing these parts. I think the most straightforward approach would to add a "Templates" item under Appearance on non-block themes when they make use of parts.

  • It's easy for a PHP template to use a block template part. The tricky part is how should it inform that it's doing so for the admin to expose the relevant UI?
  • The relevant UI could be just a wp list view of the template post type. When editing an entry we should load a site-editor instance with just that template part in focused mode.

It'd be good to map a quick design flow on how it might work.

mtias avatar May 27 '22 10:05 mtias

@mtias

It's easy for a PHP template to use a block template part. The tricky part is how should it inform that it's doing so for the admin to expose the relevant UI?

I assume a scan of the HTML files in theme/parts like it does for FSE would suffice?

coreyworrell avatar May 28 '22 02:05 coreyworrell

It's easy for a PHP template to use a block template part. The tricky part is how should it inform that it's doing so for the admin to expose the relevant UI?

I assume a scan of the HTML files in theme/parts like it does for FSE would suffice?

The files scan is only performed when the site editor is loaded. To expose the UI in the admin menu, a more lightweight check would be needed, since it needs to run on every admin page load. I think current_theme_supports( 'block-templates' ) is appropriate in this case.

carlomanf avatar May 28 '22 11:05 carlomanf

@mtias

a PHP template to use a block template part. The tricky part is how should it inform that it's doing so for the admin to expose the relevant UI?

Could the existing templateParts key in theme.json be expanded to provide this? As an example of an existing valid templatePart with an imaginary expanded one:

{
  "version": 2,
  "templateParts": [
    {
      "name": "header",
      "title": "Header",
      "area": "header"
    },
    {
      "name": "large-overlay-message",
      "title": "Large Overlay Message",
      "area": "overlay",
      "isolatedEditor": { 👈 This makes the part editable in isolation in Appearance > Editor > Template Parts.
        "allowedBlocks": [ 👈 Template parts should have top-level block restrictions.
          "core/heading",
          "core/paragraph"
        ],
        "layout": {
          "contentSize": "768px" 👈 This sets an initial editor width.
        }
      }
    }
  ]
}

All of this functionality would be so much more useful with a way to restrict top-level blocks to the intention of the template part. Something like:

<!-- wp:template-part {"slug":"tiny-greeting","tagName":"aside", "allowedBlocks": ["core/heading", "core/paragraph"]} /-->

ALJ avatar May 28 '22 20:05 ALJ

I think the most straightforward approach would to add a "Templates" item under Appearance on non-block themes when they make use of parts.

Sharing a quick flow for this appoach:

Appearance _ Templates

If we wanted to provide a way for users to set/change template parts from the post editor, a “Templates” panel could be added to the document settings tab for specifying what template part should appear in a particular area:

Post Editor

Would still need to think through how a change could be saved in that case (which would normally be saved to the Template) but wanted to check in to see if this aligns with what folks have in mind so far!

critterverse avatar May 30 '22 11:05 critterverse

The idea of allowing users to change which header/footer template should get used is interesting and not something I have previously considered. I can see how that would be useful for some folks.

From my perspective especially in more controlled client environments, it would not even be necessary to allow editors to create new template parts. Since they would always need to get used in a PHP template first and so the utility of a template part that doesn't get used is limited.

The feature set that would be super useful for these themes that only start to gradually adopt FSE is showing uses the various templates that the theme supports / has already defined and then allowing the users to modify the contents of each of these various templates.

fabiankaegy avatar May 30 '22 12:05 fabiankaegy

If we wanted to provide a way for users to set/change template parts from the post editor, a “Templates” panel could be added to the document settings tab

@critterverse Considering the label "Template" is pulling a lot of weight in WordPress at the moment (block templates, page templates), should we use the already-defined "Template Parts", instead? Or is it intentional to refer to "Template Parts" as "Templates" in the context of PHP themes?

I'll echo Fabian's note: for the controlled client sites I work on, it's not necessary to allow editors to create new parts. The most important thing is to provide a way to expose the block editor to ad-hoc theme-defined "Template Parts".

Although not essential, defining (1) allowedBlocks and (2) an initial editor width, would make this feature much more useful to smoothly guide content editors when editing the template parts.

ALJ avatar May 31 '22 16:05 ALJ

Good to know about not needing a pathway to create new Template Parts, thanks @fabiankaegy and @ALJ. I've gone ahead and removed that aspect from the mockups above.

critterverse avatar May 31 '22 19:05 critterverse