Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Try using slot fill for template content
  • Loading branch information
gziolo committed May 19, 2023
commit 7f17cf4eaecbbe63391c2f17211d696f46d8186f
6 changes: 3 additions & 3 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,11 +827,11 @@ A cloud of your most used tags. ([Source](https://github.com/WordPress/gutenberg
- **Supports:** align, anchor, spacing (margin, padding), typography (lineHeight), ~~html~~
- **Attributes:** largestFontSize, numberOfTags, showTagCounts, smallestFontSize, taxonomy

## Template Fill
## Template Content

Add custom template fill to use with the template slot. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/template-fill))
Use template content to replace the template placeholder. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/template-content))

- **Name:** core/template-fill
- **Name:** core/template-content
- **Category:** widgets
- **Supports:** ~~className~~, ~~customClassName~~, ~~html~~, ~~inserter~~
- **Attributes:** content, name
Expand Down
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function gutenberg_reregister_core_block_types() {
'spacer',
'table',
'table-of-contents',
'template-fill',
'template-content',
'text-columns',
'verse',
'video',
Expand Down
6 changes: 3 additions & 3 deletions lib/experimental/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'',
array(
'<!-- wp:paragraph {"fontSize":"huge"} -->',
'<p class="has-huge-font-size"></$wp:template-slot name="getInTouch"></p>',
'<p class="has-huge-font-size"></$wp:template-content name="getInTouch"></p>',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the / in the right place for $wp:template-content?

Copy link
Member Author

@gziolo gziolo May 24, 2023

Choose a reason for hiding this comment

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

I was experimenting with the idea of the tag closer that gets converted to the HTML comment in the browser. In effect, if you never replace </$wp:template-content name="getInTouch"> with the content, the block will still get rendered correctly for the site editors because it will simply skip this syntax for the content placeholder.

@dmsnell came up with the idea of using something like </$wp:template-content>. The interesting aspect of it is that it can be matched with the HTML tag processor when visiting closing HTML tags.

'<!-- /wp:paragraph -->',
'<!-- wp:columns -->',
'<div class="wp-block-columns"><!-- wp:column -->',
Expand All @@ -33,9 +33,9 @@
'<div class="wp-block-button"><a class="wp-block-button__link has-dark-gray-background-color has-background">' . esc_html__( 'Contact Us', 'default' ) . '</a></div>',
'<!-- /wp:button --></div>',
'<!-- /wp:buttons -->',
'<!-- wp:template-fill {"name":"getInTouch"} -->',
'<!-- wp:template-content {"name":"getInTouch"} -->',
esc_html__( 'Get In Touch', 'default' ),
'<!-- /wp:template-fill -->',
'<!-- /wp:template-content -->',
)
),
)
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export {
RichTextToolbarButton,
__unstableRichTextInputEvent,
} from './rich-text';
export { default as __experimentalTemplateContent } from './template-content';
export { default as ToolSelector } from './tool-selector';
export { default as __experimentalUnitControl } from './unit-control';
export { default as URLInput } from './url-input';
Expand Down
14 changes: 13 additions & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { useInsertReplacementText } from './use-insert-replacement-text';
import { useFirefoxCompat } from './use-firefox-compat';
import FormatEdit from './format-edit';
import { getMultilineTag, getAllowedFormats } from './utils';
import TemplateContent from '../template-content';

export const keyboardShortcutContext = createContext();
export const inputEventContext = createContext();
Expand Down Expand Up @@ -318,7 +319,7 @@ function RichTextWrapper(
}

const TagName = tagName;
return (
const result = (
<>
{ isSelected && (
<keyboardShortcutContext.Provider value={ keyboardShortcuts }>
Expand Down Expand Up @@ -415,6 +416,17 @@ function RichTextWrapper(
/>
</>
);

if ( ! originalValue.startsWith( '<!--$wp:template-content' ) ) {
return result;
}

return (
<div { ...props }>
<TemplateContent.Slot bubblesVirtually />
Hello!
</div>
);
}

const ForwardedRichTextContainer = forwardRef( RichTextWrapper );
Expand Down
10 changes: 10 additions & 0 deletions packages/block-editor/src/components/template-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* WordPress dependencies
*/
import { createSlotFill } from '@wordpress/components';

const { Fill: TemplateContent, Slot } = createSlotFill( 'TemplateContent' );

TemplateContent.Slot = Slot;

export default TemplateContent;
4 changes: 2 additions & 2 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import * as spacer from './spacer';
import * as table from './table';
import * as tableOfContents from './table-of-contents';
import * as tagCloud from './tag-cloud';
import * as templateFill from './template-fill';
import * as templateContent from './template-content';
import * as templatePart from './template-part';
import * as termDescription from './term-description';
import * as textColumns from './text-columns';
Expand Down Expand Up @@ -161,7 +161,7 @@ const getAllBlocks = () => {
pageList,
pageListItem,
pattern,
templateFill,
templateContent,
preformatted,
pullquote,
reusableBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"__experimental": true,
"name": "core/template-fill",
"title": "Template Fill",
"name": "core/template-content",
"title": "Template Content",
"category": "widgets",
"description": "Add custom template fill to use with the template slot.",
"description": "Use template content to replace the template placeholder.",
"textdomain": "default",
"attributes": {
"content": {
Expand Down
35 changes: 35 additions & 0 deletions packages/block-library/src/template-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
RichText,
useBlockProps,
__experimentalTemplateContent as TemplateContent,
} from '@wordpress/block-editor';

function TemplateContentEdit( {
attributes: { content, name },
setAttributes,
} ) {
const blockProps = useBlockProps();

return (
<>
<RichText
{ ...blockProps }
identifier="content"
tagName={ 'div' }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
aria-label={ __( 'Template content' ) }
placeholder={ __( 'Template content' ) }
/>
<TemplateContent name={ name }>Foo!</TemplateContent>
</>
);
}

export default TemplateContentEdit;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*/
import { RawHTML } from '@wordpress/element';

export default function save( { attributes: { content } } ) {
export default function templateContentSave( { attributes: { content } } ) {
return <RawHTML>{ content }</RawHTML>;
}
25 changes: 0 additions & 25 deletions packages/block-library/src/template-fill/edit.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/blocks/src/api/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ function getHTMLTokens( html, logger = createLogger() ) {
let temp = html;
try {
// Quick way to ensure that the template slot is correctly validated as HTML comment.
if ( temp.includes( '</$wp:template-slot' ) ) {
if ( temp.includes( '</$wp:template-content' ) ) {
temp = temp.replace(
/<\/(\$wp\:template-slot[^>]*)>/gi,
/<\/(\$wp\:template-content[^>]*)>/gi,
'<!-- $1 -->'
);
}
Expand Down