benjgrolleau / guttemplates
使用面向对象的API创建带有块或块模式的帖子模板。
1.0.3
2022-04-20 18:01 UTC
Requires (Dev)
- php: >=7.3
- symfony/var-dumper: ^5.4
This package is not auto-updated.
Last update: 2024-09-30 21:10:02 UTC
README
使用面向对象的方法为CPT注册自定义模板。本包为您提供了面向对象的API,以便将模板声明到您的CPT中,并使块模式更加简单。
作者
- @benj_grolleau 感谢 Florian Truchot、Marie Comet、Dareth Nhang 和 Alexandre Buffet 在开发过程中的意见分享。
安装
使用composer安装。
composer require benjgrolleau/guttemplates
注意:您可以使用Composer 2安装FPP。
composer self-update --2
如果您想回滚到Composer 1,请使用
composer self-update --1
文档
声明块模式
使用函数declare_block_pattern
。
use Gut\Templates\Blocks\Column;
use Gut\Templates\Blocks\Columns;
use Gut\Templates\Blocks\Paragraph;
use Gut\Templates\Blocks\ImageBlock;
register_block_pattern(
'fpp/custom-pattern',
array(
'title' => __( 'Custom pattern', 'faster-printing-press' ),
'description' => __( 'Example block.', 'faster-printing-press' ),
'categories' => array( 'content' ),
'content' => declare_block_pattern([
Columns::create()
->inner([
Column::create(),
->inner([
ImageBlock::create()
])
Column::create()
->inner([
Paragraph::create()
->content( 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Sunt, qui?' ),
]),
]),
]),
)
);
声明帖子类型的模板
使用函数declare_post_type_template
。
use Gut\Templates\Blocks\Column;
use Gut\Templates\Blocks\Columns;
use Gut\Templates\Blocks\Paragraph;
use Gut\Templates\Blocks\ImageBlock;
use Gut\Templates\Blocks\Heading;
use Gut\Templates\Blocks\Quote;
register_post_type( 'custom-post-type',
array(
...
'template' => declare_post_type_template([
Columns::create()
->inner([
Column::create()
->inner([
Paragraph::create()
->content( 'Here you can write some text'),
ImageBlock::create(),
Paragraph::create()
->content( 'Another paragraph ?' )
]),
Column::create()
->inner([
Quote::create()
->content( 'Knowledge is power.' )
->author( 'Francis Bacon' ),
Heading::create()
->level(1)
->color( 'primary' )
->placeholder( 'Your title goes here.' )
])
])
])
)
);
使用您自己的块
您可以使用自己的块轻松扩展。
namespace Gut\Templates\Blocks;
use Gut\Templates\Blocks\Block;
use Gut\Templates\Blocks\Attributes\Color;
class CustomBlock extends Block {
use Color;
use ClassName;
protected $name = 'custom-block';
protected $tag = 'div';
}
如果您想重写渲染函数,请使用方法renderPattern()
进行模式渲染,以及使用renderTemplate()
进行帖子类型模板的渲染。
反馈
如果您有任何反馈,请通过hello@benjamin-grolleau.fr联系我。这是我的第一个开源项目,所以请不要犹豫,给我您的反馈。在Twitter上自由打招呼!👋
路线图
- 添加所有内容块。
- 添加一个CLI来生成FSE模板。
参考
他们不知道,但他们在帮助我构建这个项目。
- WordPlate 团队,以及受我启发的Extended ACF库。
- readme.so,它帮助我编写您现在正在阅读的内容。
买我一杯咖啡 ☕️
如果您喜欢它,您可以在这里支持我的工作!谢谢。