简体   繁体   English

Timber + Gutenberg ACF Blocks 不渲染前端

[英]Timber + Gutenberg ACF Blocks doesnt rendering front end

here is my Class what extend on Timber Site class.这是我的 Class,它在木材站点 class 上扩展。

<?php 

    class EngineACF  extends Timber\Site {

        public function __construct() {
            parent::__construct();
            add_action( 'admin_head' , [$this, 'adminStyle']);
            add_action( 'acf/init', [$this, 'acfBlocks'] );
        }   

        /**
         * Define Gutenberg Blocks
         * @version 1.0
         */

        public function acfBlocks()
        {
            if (!function_exists( 'acf_register_block' )) {
                return;
            }

            $blocks = [
                [
                    'name' => 'homepage-slider',
                    'title' => 'Homepage Slider'
                ]
            ];

            foreach ($blocks as $perKey => $perBlock) {
                acf_register_block(
                    [
                        'name'            => data_get($perBlock, 'name'),
                        'title'           => data_get($perBlock, 'title'),
                        'description'     => data_get($perBlock, 'title'),
                        'render_callback' => [$this, 'renderAcfCallback'],
                        'category'        => 'formatting',
                        'icon'            => 'format-aside',
                        'keywords'        => [str_replace(' ', ',', data_get($perBlock, 'name'))],
                    ]
                );
            }
        }

        /**
         * Render Dynamic Gutenberg block template
         * @version 1.0
         */

        public function renderAcfCallback( $block, $content = '', $is_preview = false ) {
            
            $blockName = data_get($block, 'name');

            $blockName = str_replace('acf/', '', $blockName); 
            $context                = Timber::context();
            $context['block']       = $block;
            $context['fields']      = get_fields();
            $context['is_preview']  = $is_preview;
 
            Timber::render( 'blocks/'.$blockName.'-block.twig', $context );
        }
 
    }

I also have, blocks/homepage-slider-block.twig file.我也有,blocks/homepage-slider-block.twig 文件。 In admin panel, with gutenberg editor everything works perfectly.在管理面板中,使用古腾堡编辑器一切正常。 But in frontend, post content doesn't work/render correctly.但在前端,帖子内容无法正常工作/呈现。

Any helps?有什么帮助吗? Thanks.谢谢。

The output like: output 像:

前端输出

Allright.好的。 I just found what the issue was.我刚刚发现问题出在哪里。 I was using the post.post_content variable from twig.我使用的是 twig 中的post.post_content变量。

Apparently that is not a variable that contains the final content .显然那不是一个包含最终内容的变量

For future reference for people with something like this issue: Use the {{post.content}} variable.供有类似问题的人将来参考:使用{{post.content}}变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM