定制 hex-digital/sage-blade-block-renderer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hex-digital/sage-blade-block-renderer

Composer 安装命令:

composer require hex-digital/sage-blade-block-renderer

包简介

Generate blade partials to render native gutenberg blocks with Sage 10

README 文档

README

Latest Stable Version Total Downloads Build Status

The Blade Block Renderer for Sage allows the easy registration of blade partials for the render function of native Gutenberg blocks.
Useful when you want to edit a block in React, but render with Blade.

Features

  • 🔥 Use all your blade components and PHP functions immediately in your native Gutenberg blocks.
  • 🔥 Instantly generate a working render_callback, powered by Blade with a native Sage 10 feel for passing view data.
  • 🔥 All blocks support InnerBlocks content

Requirements

Installation

Install via Composer:

$ composer require hex-digital/sage-blade-block-renderer

Usage

Create your native block

Create your Gutenberg block as you normally would, with one small change.

Instead of adding HTML to the save function of your block, instead define it as one of the following:

// If you have InnerBlocks content:
save: function (props) {
  return <InnerBlocks.Content/>;
}

// If there is no InnerBlocks content:
save: function() {
  return null;
}

And that's it! When your block is saved, Gutenberg will save the settings to the database. When it comes time to render your block, the settings will be given to the Blade Block Renderer, and your blade template will be used, along with any attributes, classNames or Inner Blocks given.

If you prefer to use Blade for both the edit and save HTML, you would benefit from using Log1x's ACF Composer package instead. This similarly provides Blade support for blocks, but utilises ACF's Blocks instead, which will be much more powerful.

Rendering a Block with a blade-powered render callback

To create your first rendered block, start by running the following generator command from your theme directory:

$ wp acorn blade-block Example

This will create app/BladeBlocks/Example.php which is where you will create and manage your first blade block:

<?php

namespace Example;

use BladeBlock\BladeBlock;

class Example extends BladeBlock
{
    /**
     * The block slug. Should match the slug given to your registered block.
     *
     * @var string
     */
    public $slug = 'example';

    /**
     * Data to be passed to the block before rendering.
     *
     * @param BladeBlock $block
     * @return array
     */
    public function with($block)
    {
        return [
            'data' => 'dummy data',
        ];
    }
}

You'll see it has a familiar with() function for passing data to your block. However, it is also given the block object as the first parameter.

This allows you to get any attribute, setting or content from the block, as required.

For example, $block->content will give the Inner Blocks content, and $block->image_id would give the value of an attribute called image_id that was defined on the block in JavaScript.

A View is also generated in the resources/views/blocks directory:

<div class="{{ $block->classes }}">
  @if ($data)
    <p>{!! $data !!}
  @else
    <p>No data found!</p>
  @endif

  @if ($content)
    {!! $content !!}
  @endif
</div>

The $content variable will contain the HTML for any InnerBlocks added in the editor.
The $blockClass variable will contain the base class for the block, for use with BEM class naming.
This is in the form "wp-block-$prefix-$slug" E.G. wp-block-hex-page-header.
These can then be outputted as required into the blade partial.

All block data is available on the $block object, which is the same as that passed to the BladeBlock with() function. All data returned from the with() function is also available.

Block Preview View

In the view file, you can use $block->preview for conditionally modifying your block when shown in the editor.

You can also load a different blade partial by duplicating your existing view and prefixing it with preview- (e.g. preview-example.blade.php).

However, if you're using the blade partial for both the edit and save HTML, you would benefit from using Log1x's ACF Composer package instead. This similarly provides Blade support for blocks, but utilises ACF's Blocks instead, which will be much more powerful.

Modifying your Block

Your Block has a few options available to it to modify. Add these as member variables to your generated block class to define and use them.

Your block's class will default to "wp-block-$prefix-$slug", where the prefix has the trailing slash trimmed. This block class is available in the view as $blockClass for easy use with BEM class naming.

/**
 * The block prefix. Should match the prefix given to your block
 *
 * @var string
 */
public $prefix = 'hex/';

/**
 * The block slug. Should match the slug given to your registered block.
 *
 * @var string
 */
public $slug = '';

/**
 * The block view. Same format as given to the blade `include()` function. A dot-separated path where the root is `resources/views`.
 * If left blank, defaults to `'blocks.' . $this->slug`.
 *
 * @var string
 */
public $view;

Bug Reports

If you discover a bug in Sage Blade Block Renderer, please open an issue.

Contributing

Contributing, whether through PRs, reporting an issue, or suggesting an idea, is encouraged and extremely appreciated.

License

We provide the Sage Blade Block Renderer under the MIT License.

hex-digital/sage-blade-block-renderer 适用场景与选型建议

hex-digital/sage-blade-block-renderer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.25k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hex-digital/sage-blade-block-renderer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hex-digital/sage-blade-block-renderer 我们能提供哪些服务?
定制开发 / 二次开发

基于 hex-digital/sage-blade-block-renderer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-19