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

Suggestion: Block templates in page template header #3835

Closed
3 tasks
Sephsekla opened this issue Dec 6, 2017 · 8 comments
Closed
3 tasks

Suggestion: Block templates in page template header #3835

Sephsekla opened this issue Dec 6, 2017 · 8 comments
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor
Milestone

Comments

@Sephsekla
Copy link
Contributor

Although blocks may in many cases replace traditional page templates, there are still a number of cases where different templates are needed

Automatically adding blocks on registering a CPT works well, this could be extended to page templates.

Possible Solution

As page templates are automatically included by WordPress rather than through an action (as with CPTs), this could potentially be controlled by the header comments that already dictate the template name.

<?php 
/*
Template Name: Full-width layout
Template Post Type: post, page, event
Allowed Blocks: core/image, core/paragraph, core/quote
Initial Blocks: array(
		array( 'core/image' ),
		array( 'core/paragraph', array(
			'placeholder' => 'Add a book description',
		) ),
		array( 'core/quote' )
Template Lock: all

*/
// Page code here...

Alternatively, it may be preferable to keep the initial blocks as an action instead, with a new function like the below:

register_template_blocks( $page_template, $args = array(

'template' => array(
		    array( 'core/image' ),
		    array( 'core/paragraph', array(
			    'placeholder' => 'Add a book description',
		     ) ),
		    array( 'core/quote' ),
	    ),
	    'template_lock' => 'all'
  )
  );

Todos

  • Implementation
  • Tests
  • Documentation
@jaswrks
Copy link
Contributor

jaswrks commented Dec 7, 2017

Interesting idea. See also: #3588

@johnbillion
Copy link
Member

This is a powerful concept and one which is seen in several existing field libraries such as ACF and CMB2. I'd love to see it in Gutenberg.

@mtias
Copy link
Member

mtias commented Jan 9, 2018

The plan for pages was to introduce a function like do_blocks( $template ). The key thing with page templates is that we want all of it to be potentially covered by blocks, not just the_content.

In the meantime, a solution like one of the above might make sense as just populating the content area. If anyone wants to look at a proof implementation, that'd be great.

@mtias mtias added the [Feature] Templates API Related to API powering block template functionality in the Site Editor label Jan 9, 2018
@wpscholar
Copy link
Member

The problem I see with this is trying to implement a PHP array in a comment. That will be tedious and prone to error.

Perhaps we could have a function where we register block templates with an internal name, much like how we register post types? Then we could just reference the internal name for the block template and WordPress will have already loaded it. This would also make it easier if someone needed to use (or change) the same block template across different page templates (or post types).

@oxyc
Copy link
Member

oxyc commented Jun 12, 2018

In case someone needs a feature like this I iterated on the work of @jasonbahl in #3588 and made a PoC plugin for my own use case https://github.com/generoi/wp-gutenberg-templates. I wont be maintaining it and will look to deprecate it for my own use cases once Gutenberg has a better alternative.

At the moment you register a template using:

add_action('init', function () {
    register_gutenberg_template('foobar', [
        'post_type' => 'page',
        'name' => __('Foobar'),
        'template' => [
            ['core/image', ['align' => 'left'],
            ['core/paragraph', ['placeholder' => __('Foobar')],
        ],
        'template_lock' => 'all',
    ]);
});

It will then show up as a page template option even if no template file exists (regular template fallback applies) and read the template definition when the Page template attribute changes in the Gutenberg editor.

I added some basic validation that prompts for confirmation before replacing existing content but at the moment everything is very untested. Just thought I'd mention it if someone wants a starting point

@mtias mtias mentioned this issue Aug 1, 2018
16 tasks
@aaronjorbin aaronjorbin added this to the API Freeze milestone Oct 8, 2018
@aaronjorbin
Copy link
Member

I could see this slipping to future, but a decision should be made before the API freeze

@youknowriad
Copy link
Contributor

This is definitely a phase2 thing and I don't want it to be rushed.

@youknowriad
Copy link
Contributor

Closing this. This is the whole idea of FSE. I encourage everyone to follow the label for updates https://github.com/WordPress/gutenberg/issues?q=is%3Aissue+is%3Aopen+label%3A%22%5BFeature%5D+Full+Site+Editing%22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor
Projects
None yet
Development

No branches or pull requests

8 participants