Gutenberg:无法使用ServerRender保存属性

时间:2018-11-18 作者:jsnoob

我已经为这个问题挣扎了3个小时,我看不到结束。问题:我试图用ServerSideRender在前端显示post\\u元值。该值显示在Gutenberg编辑器中,但不显示在前端。

Problem

我用脚手架搭了一个新的街区Create Guten Block

我的街区。js公司

/**
 * BLOCK: bold-blocks
 *
 * Registering a basic block with Gutenberg.
 * Simple block, renders and saves the same content without any interactivity.
 */

//  Import CSS.
import \'./style.scss\';
import \'./editor.scss\';

const {__} = wp.i18n; // Import __() from wp.i18n
const {registerBlockType} = wp.blocks; // Import registerBlockType() from wp.blocks
const {ServerSideRender} = wp.components;


registerBlockType(\'cgb/block-bold-blocks\', {
    // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
    title: __(\'bold-blocks - Example Meta Content\'), // 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: [
        __(\'bold-blocks — CGB Block\'),
        __(\'CGB Example\'),
        __(\'create-guten-block\'),
    ],

    attributes: {
        metaToDispaly: {
            type: \'string\',
            source: \'meta\',
            meta: \'werk\',
            default: \'30\'
        }
    },
    edit: function (props) {
        // ensure the block attributes matches this plugin\'s name
        console.log(props.attributes);
        return (
            <ServerSideRender
                block="cgb/block-bold-blocks"
                attributes={props.attributes}
            />
        );
    },
    save() {
        // Rendering in PHP
        return null;
    },
});
PHP

function bold_blocks_cgb_block_assets()
{
    // Styles.
    wp_enqueue_style(
        \'bold_blocks-cgb-style-css\', // Handle.
        plugins_url(\'dist/blocks.style.build.css\', dirname(__FILE__)), // Block style CSS.
        array(\'wp-blocks\') // Dependency to include the CSS after it.
    // filemtime( plugin_dir_path( __DIR__ ) . \'dist/blocks.style.build.css\' ) // Version: filemtime — Gets file modification time.
    );
} // End function bold_blocks_cgb_block_assets().

// Hook: Frontend assets.
add_action(\'enqueue_block_assets\', \'bold_blocks_cgb_block_assets\');

function bold_blocks_cgb_editor_assets()
{
    // Scripts.
    wp_enqueue_script(
        \'bold_blocks-cgb-block-js\', // Handle.
        plugins_url(\'/dist/blocks.build.js\', dirname(__FILE__)), // Block.build.js: We register the block here. Built with Webpack.
        array(\'wp-blocks\', \'wp-i18n\', \'wp-element\'), // Dependencies, defined above.
        // filemtime( plugin_dir_path( __DIR__ ) . \'dist/blocks.build.js\' ), // Version: filemtime — Gets file modification time.
        true // Enqueue the script in the footer.
    );

    // Styles.
    wp_enqueue_style(
        \'bold_blocks-cgb-block-editor-css\', // Handle.
        plugins_url(\'dist/blocks.editor.build.css\', dirname(__FILE__)), // Block editor CSS.
        array(\'wp-edit-blocks\') // Dependency to include the CSS after it.
    // filemtime( plugin_dir_path( __DIR__ ) . \'dist/blocks.editor.build.css\' ) // Version: filemtime — Gets file modification time.
    );
} // End function bold_blocks_cgb_editor_assets().

// Hook: Editor assets.
add_action(\'enqueue_block_editor_assets\', \'bold_blocks_cgb_editor_assets\');


/**************************** MY PART ************************************************/
function bold_register_dynamic_blocks()
{
    register_block_type(\'cgb/block-bold-blocks\', array(
        \'attributes\' => array(
            \'metaToDispaly\' => array(
                \'type\' => \'string\',
            ),
        ),
        \'render_callback\' => function ($attributes) {
            return \'Attributes=\'. print_r($attributes, true );
        }
    ));

}

add_action(\'init\', \'bold_register_dynamic_blocks\');
我认为问题是,属性没有添加到post\\u内容中

这就是古腾堡产生的:

<!-- wp:cgb/block-bold-blocks /-->

这就是我认为古腾堡应该生成的内容(如果我把它放在DB字段中,它会起作用:

<!-- wp:cgb/block-bold-blocks {"metaToDispaly": "C01112107"} /-->

有人知道我做错了什么吗?

1 个回复
最合适的回答,由SO网友:jsnoob 整理而成

经过4个小时愚蠢的尝试,我得到了一个结果&;错误

我无法从源读取值meta 在前端。我将meta中的变量分配给了一个常规的字符串属性,它可以正常工作。简单的豌豆。

/**
 * BLOCK: bold-blocks
 *
 * Registering a basic block with Gutenberg.
 * Simple block, renders and saves the same content without any interactivity.
 */

//  Import CSS.
import \'./style.scss\';
import \'./editor.scss\';

const {__} = wp.i18n; // Import __() from wp.i18n
const {registerBlockType} = wp.blocks; // Import registerBlockType() from wp.blocks
const {ServerSideRender} = wp.components;
const {AlignmentToolbar, BlockControls, InspectorControls,} = wp.editor;

const {
    PanelBody,
    PanelRow,
    Fragment
} = wp.element;


registerBlockType(\'cgb/block-bold-blocks\', {
    // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
    title: __(\'bold-blocks - Example Meta Content\'), // 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: [
        __(\'bold-blocks — CGB Block\'),
        __(\'CGB Example\'),
        __(\'create-guten-block\'),
    ],

    attributes: {
        metaToDisplay: {
            type: \'string\',
            source: \'meta\',
            meta: \'werk\',

        },
        className: {
            type: \'string\',
            default: \'fuck\'
        },
        werkId: {
            type: \'string\',
            value: \'foo\',
            default: \'shit\'
        }


    },
    edit(props) {
    //    const {attributes: {metaToDisplay, className}, setAttributes, isSelected} = props;

        let metaToDisplay = props.attributes.metaToDisplay;
        let className = props.className;
        props.setAttributes( { werkId: metaToDisplay } );
        props.werkId = props.metaToDisplay;


        return (
            <ServerSideRender
                block="cgb/block-bold-blocks"
                attributes={{
                    metaToDisplay: metaToDisplay,
                    className: className,
                    werkId: metaToDisplay
                }}

            />

        )
            ;

    },


    save: function (props) {
      //  const {attributes: {metaToDisplay, className}, setAttributes} = props;
        // Rendering in PHP
        return null;

    },
});
我敢肯定,我在这里做一些货物崇拜。有人能为我的问题提供一个更优雅的解决方案吗?

结束

相关推荐

Server specification

我想知道wordpress portal的服务器规范,知道提供了以下数据:对于具有帐户的用户账户数量每年4万+1000高峰小时用户数-16000每个会话的平均请求数-350思考时间-7秒对于anonim用户用户每日-6k-8k高峰小时用户-600-800每个会话的平均请求数-10-15思考时间-7秒我一直在研究,但我找不到任何数据我应该如何计算这个。。。有人能帮忙吗?