我有一个前端带有服务器端渲染的简单块。
PHP:
register_block_type( \'some/block\', array(
\'render_callback\' => \'render_my_block\',
\'attributes\' => array(
\'stuff\' => array(
)
)
));
function render_my_block( $attributes ) {
// $attributes[\'stuff\']
return \'<h1>Hello frontend</h1>\';
}
这是可行的,但我还需要在管理区域将其呈现为预览,因此我添加了JS:registerBlockType( \'some/block\', {
title: \'Some block\',
attributes: {
stuff : {
}
},
edit( { className, attributes, setAttributes } ) {
return (
<Fragment>
<SomeInput />
<SomeOtherInput />
<Preview>
// I need to get contents of PHP function render_my_block here,
// based on current attributes.stuff
</Preview>
</Fragment>
);
},
save( { attributes } ) {
return null; // server side
}
} );
我的问题是——获取这些数据的正确方法是什么?我应该使用wp_ajax_
回调/筛选?或者古腾堡有更好的方法来处理这个问题?我已经检查了默认的“最新帖子”块的工作原理——它使用Rest API获取帖子ID和标题,然后通过react呈现它们。但对于我的情况,我只需要返回简单的HTML字符串。