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

Creating templates that fully control the content layout #7961

Closed
alexsanford opened this issue Jul 13, 2018 · 6 comments
Closed

Creating templates that fully control the content layout #7961

alexsanford opened this issue Jul 13, 2018 · 6 comments
Labels
[Feature] Templates API Related to API powering block template functionality in the Site Editor [Type] Enhancement A suggestion for improvement. [Type] Question Questions about the design or development of the editor.

Comments

@alexsanford
Copy link
Contributor

Block templates are great as a starting point for users. But plugin and theme developers also need a way for a template to completely control the layout of some content. Furthermore, this sort of controlling template must be extensible, such that when it is modified, everything across the site being rendered with the template is updated.

For example, consider a block template being used to display a CPT created by a plugin. The plugin author may wish to have the template locked such that it may only be changed at a global level, not for individual posts. This would be useful for theme developers who wish to customize how the CPT is displayed within their theme, or extension developers who wish to display additional information on the CPT's page.

One significant challenge to overcome will be to structure this in such a way that when a template is modified, any existing content (e.g. existing CPT posts) renders with the new template. Currently, if a block template is modified, existing posts do not render any differently. Only new posts have the new template applied.

(Note that although it's convenient to think of these templates with regard to posts and CPT's, it is also applicable to parent Blocks using a template to lay out their children.)

Implementation thoughts and concerns

  • The template needs to control both the Edit UI and the rendered frontend content, so that the user sees in the editor the layout that will ultimately be rendered on the frontend.

  • The content controlled by this sort of template will likely need to be rendered dynamically on the server, rather than statically from post_content. Otherwise we would have no way to change the rendered output of existing posts when the template is modified.

  • Currently, a template cannot (in general) be rendered without some frontend code being run. Consider a modified template that includes a static block. This block can only be rendered by running its save method in JavaScript. However, that method is not available on the server where the template is defined, so rendering an existing post with the new template will not be possible without some client-side code also being run.

  • Editable blocks within a template will likely need to have a "key" or some sort of unique identifier associated with them. This way, when a template is modified and blocks are moved around, the existing content can be displayed in the correct location.

Would love to start to have some discussion around this. I think it's a "phase two" thing, but it's a pretty important and challenging problem that we'll need to get right in order to make Gutenberg work well for theme and plugin developers.

@designsimply designsimply added [Type] Question Questions about the design or development of the editor. Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Templates API Related to API powering block template functionality in the Site Editor and removed Backwards Compatibility Issues or PRs that impact backwards compatability labels Jul 18, 2018
@rogerlos
Copy link

A huge step forward in this context would be to simply allow a template to have an assigned "id" which would then be added to the template wrapper as a class.

$block = [
    'template' => [
        [ 'block/name' ],
    ],
];

Becomes...

$block = [
   'template' => [
        'id' => 'my_lackluster_template',
        'blocks' => [
              [ 'block/name' ],
         ]
    ]
];

...which opens templates to be extensible via properties.

Maybe eventually including something like the ability to reference other templates:

$block = [
    'template' => [
        'id' => 'my_lackluster_template',
        'blocks' => [
            [ 'objects_are_blocks' ],
            'strings_reference_another_template',
            [ 'etc' ],
        ]
    ]
];

Setting aside that particular fever-dream, in the editor:

<div class="editor-block-list__block-edit template-my_lackluster_template" data-block="...">
...
</div>

Given that structure, we can now add CSS which specifically targets the template, both in-editor and on the front end.

@rogerlos
Copy link

And maybe a block's template should be an array of template objects, which can contain contextual information about which to use where. I suppose you could argue that you're in "define new block" territory at that point...but if so you cannot then have multiple defined templates for, say, custom post types...which I can definitely see a use-case for.

@nerrad nerrad added the [Type] Enhancement A suggestion for improvement. label Aug 11, 2019
@paaljoachim
Copy link
Contributor

Could we get a status update?
Thanks.

@iandunn
Copy link
Member

iandunn commented Mar 9, 2021

@designsimply, @nerrad, do you think think might fit into FSE?

@nerrad
Copy link
Contributor

nerrad commented Mar 10, 2021

I'm a bit behind in familiarity with all that is planned/is possible for FSE and whether it would meet the needs of this issue unfortunately. On the surface though, this looks like it should.

@paaljoachim
Copy link
Contributor

Regarding Page templates. Here is a PR that is being worked on:
#30438

@mtias mtias closed this as completed Jul 13, 2021
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 [Type] Enhancement A suggestion for improvement. [Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

No branches or pull requests

7 participants