PHPackages                             tiny-pixel/blocks - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tiny-pixel/blocks

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tiny-pixel/blocks
=================

For consummate blockbuilders

1.0.2(4y ago)32626MITPHPPHP &gt;=7.1CI failing

Since Nov 29Pushed 2y ago6 watchersCompare

[ Source](https://github.com/pixelcollective/tiny-blocks)[ Packagist](https://packagist.org/packages/tiny-pixel/blocks)[ Patreon](https://www.patreon.com/user?u=27947550)[ RSS](/packages/tiny-pixel-blocks/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (5)Dependencies (5)Versions (7)Used By (0)

tiny-pixel/blocks
=================

[](#tiny-pixelblocks)

Provides a backbone for building modular blocks with Blade templating support.

This framework is under active development.

What this is
------------

[](#what-this-is)

- A way to streamline and structure the registration of blocks
- A way to streamline and structure the registration of block assets
- A provider of Blade functionality and templating utilities for server-rendered, dynamic blocks.

What this is not
----------------

[](#what-this-is-not)

- A way of writing editor blocks without javascript

Getting started
---------------

[](#getting-started)

Use `composer` to install the package to your project:

```
composer require tiny-pixel/blocks
```

Usage
-----

[](#usage)

See the demo directory in the repo for an example implementation.

File structure:

```
demo
├── config
│   └── app.php # Plugin config
├── index.php # Plugin entrypoint
├── resources
│   ├── assets # JS and CSS assets
│   │   # Implementation is up to you
│   │
│   └── views # Place blade templates here
│       └── Demo # Should match block name
│           └── block.blade.php # Entry template
│      
└── src # Block classes
    └── Demo.php # Match name of views & class
```

Somewhere in your plugin, hook into the `tinypixel/blocks/loaded` action.

It passes you an instance of the library. Call `main` to kick everything off. You must pass the path to your config directory to `main`.

```
add_action('tinypixel/blocks/loaded', function ($app) {
    $configPath = realpath(__DIR__ . '/config');
    $app->main($configPath);
});
```

In `config/app.php`, you only really need to do two things:

1. Add your blocks to the classes in the `blocks` key:

```
   'blocks' => [
      \Foo\Demo::class,
   ],
```

2. Change the `project.domain` key to match the namespace of your blocks. Not the PHP namespace, but the block namespace used in `register_block_type`.

```
   'project' => [
        'domain' => 'foo',
        'base_path' => realpath(__DIR__ . '/../'),
        'base_url' => plugins_url('/', __DIR__),
        'dist' => 'dist',
   ],
```

You can modify the other settings as you see fit. They are largely self explanatory and control where caches are stored, where compiled assets are being stored, etc. One day this might even be documented!

Block definitions
-----------------

[](#block-definitions)

By default, the library will look for block classes in `src/`.

This classname should match the name of the block without the block namespace. For example, for a `foo/demo` namespace we will register the class as `Demo`. The namespace is set in `config.project.domain`, as mentioned above.

Use the `setupAssets` method to register your block scripts and styles.

In addition to the ones below you can use `$this->addPublicScript` and `$this->addPublicStyle` to register frontend scripts and styles.

```
