【发布时间】:2021-08-24 12:04:46
【问题描述】:
我想使用带有 create-guten-block 的 gutenberg 服务器端渲染一个简单的模板。
init.php:
function melaniemueller_templateparts_block_cgb_block_assets() {
register_block_type(
'cgb/block-melaniemueller-templateparts-block', array(
'render_callback' => 'mmu_block_render',
)
);
}
}
// Hook: Block assets.
add_action( 'init', 'melaniemueller_templateparts_block_cgb_block_assets' );
function mmu_block_render($attr, $content) {
locate_template( 'custom\php\templates\header\header_main_1.php', true );
}
block.js:
registerBlockType( 'cgb/block-melaniemueller-templateparts-block', {
// Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
title: __( 'melaniemueller-templateparts-block - CGB Block' ), // Block title.
icon: 'shield', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/.
category: 'common', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
keywords: [
__( 'melaniemueller-templateparts-block — CGB Block' ),
__( 'CGB Example' ),
__( 'create-guten-block' ),
],
edit: function( { attributes } ) {
return (
<ServerSideRender
block="cgb/block-melaniemueller-templateparts-block"
attributes={ attributes }
/>
);
},
save: function( { } ) {
return null;
},
} );
但是,如果我想渲染这部分,我会在 Chrome 的 Quirks-Mode 中的 edit-function 中遇到一个错误。
Error loading block: The response is not a valid JSON response.
有人知道我做错了什么吗?
【问题讨论】:
-
您需要分享您的代码(或至少是 Minimal, Reproducible Example),以便我们提供帮助。否则,我们只能根据您提供的信息进行猜测。
-
对不起.....感谢您的更新。我更新了我的示例。
-
@Melanie 我想你忘了在渲染回调中返回一些东西 (
mmu_block_render)。 -
已经在
locate_templatesource中返回了
标签: wordpress wordpress-gutenberg gutenberg-blocks wordpress-plugin-creation project-gutenberg