Download the PHP package hex-digital/sage-blade-block-renderer without Composer
On this page you can find all versions of the php package hex-digital/sage-blade-block-renderer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hex-digital/sage-blade-block-renderer
More information about hex-digital/sage-blade-block-renderer
Files in hex-digital/sage-blade-block-renderer
Package sage-blade-block-renderer
Short Description Generate blade partials to render native gutenberg blocks with Sage 10
License MIT
Informations about the package sage-blade-block-renderer
Sage: Blade Block Renderer
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
- Sage >= 10.0
Installation
Install via Composer:
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:
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:
This will create app/BladeBlocks/Example.php
which is where you will create and manage your first blade block:
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:
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.
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.