-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Pattern: Experiment with using template slots for content replacement #50777
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for pattern APIs present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
register_block_pattern( | ||
'gutenberg/get-in-touch', | ||
array( | ||
'title' => esc_html__( 'Get In Touch', 'default' ), | ||
'categories' => array( 'call-to-action' ), | ||
'content' => implode( | ||
'', | ||
array( | ||
'<!-- wp:paragraph {"fontSize":"huge"} -->', | ||
'<p class="has-huge-font-size"></$wp:template-content name="getInTouch"></p>', | ||
'<!-- /wp:paragraph -->', | ||
'<!-- wp:columns -->', | ||
'<div class="wp-block-columns"><!-- wp:column -->', | ||
'<div class="wp-block-column"><!-- wp:paragraph -->', | ||
'<p>' . esc_html__( '20 Cooper Avenue', 'default' ) . '<br>' . esc_html__( 'New York, New York 10023', 'default' ) . '</p>', | ||
'<!-- /wp:paragraph --></div>', | ||
'<!-- /wp:column -->', | ||
'<!-- wp:column -->', | ||
'<div class="wp-block-column"><!-- wp:paragraph -->', | ||
'<p>' . esc_html__( '(555) 555-5555', 'default' ) . '<br><a href="mailto:example@example.com">' . esc_html__( 'example@example.com', 'default' ) . '</a></p>', | ||
'<!-- /wp:paragraph --></div>', | ||
'<!-- /wp:column --></div>', | ||
'<!-- /wp:columns -->', | ||
'<!-- wp:buttons -->', | ||
'<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"dark-gray"} -->', | ||
'<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-content {"name":"getInTouch"} -->', | ||
esc_html__( 'Get In Touch', 'default' ), | ||
'<!-- /wp:template-content -->', | ||
) | ||
), | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/block-editor/src/components/template-content/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"__experimental": true, | ||
"name": "core/template-content", | ||
"title": "Template Content", | ||
"category": "widgets", | ||
"description": "Use template content to replace the template placeholder.", | ||
"textdomain": "default", | ||
"attributes": { | ||
"content": { | ||
"type": "string", | ||
"source": "html" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"supports": { | ||
"customClassName": false, | ||
"className": false, | ||
"html": false, | ||
"inserter": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import initBlock from '../utils/init-block'; | ||
import edit from './edit'; | ||
import metadata from './block.json'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
|
||
export { metadata, name }; | ||
|
||
export const settings = { | ||
edit, | ||
save, | ||
}; | ||
|
||
export const init = () => initBlock( { name, metadata, settings } ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { init } from './'; | ||
|
||
export default init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RawHTML } from '@wordpress/element'; | ||
|
||
export default function templateContentSave( { attributes: { content } } ) { | ||
return <RawHTML>{ content }</RawHTML>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.