Download the PHP package codelight/acf-blocks without Composer
On this page you can find all versions of the php package codelight/acf-blocks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package acf-blocks
ACF Blocks
ACF Blocks is a lightweight library that provides a clean, object-oriented API to create and render ACF field groups.
Your WordPress code base doesn't have to be a mess.
This library is in beta. Use at your own risk. Contributions are welcome.
Overview
ACF Blocks introduces the concept of blocks, which are essentially Controllers (or actually ViewModels) for field groups and flexible layouts. Fields are created using the excellent ACF Builder library.
The three main benefits of using ACF blocks are:
- Speed up development of simple sites,
- Provide a super simple, but clean architecture for developing more complex sites,
- Allow re-using blocks between projects.
Installation
If you're still not using Composer in 2018, then do yourself a huge favor and get started now. [todo: article]
Example 1: Quick procedural blocks
As an example, let's go through creating and rendering a simple field group.
Example 2: Encapsulate the block in a class
Let's create the same block in a much cleaner way - as a class. This class should be in a separate file called ImageBlock.php. You'll probably want to keep it in a separate folder, which you might want to call 'blocks'.
We'll also need to register the block we just created. This goes into your functions.php (or equivalent):
And that's it. You'll also need to add the templates as in the previous example.
Example 3: Setting up flexible layouts
Let's continue the previous example, but register the ImageBlock as a Flexible Content layout.
First, we'll need to create the Flexible Content block which will contain our ImageBlock.
This flexible content block works exactly as any other block. To register it, modify the code you previously added to your functions.php (or equivalent) as follows:
Now, you will have a flexible content area on every Page where you can add the ImageBlock. Note that the ImageBlock will still be added to template-image.php as a regular (non-flexible-layout) block as well. The ImageBlock will use the same template in both cases. This provides an easy way to re-use blocks between templates, flexible content areas and even projects. It's also possible to use a different template in different situations (e.g. flexible layout vs regular page context), whilst keeping the backend code of the block the same.
Example 4: So why is this useful?
One obvious answer is that once you get the general idea, it's about 10x faster compared to writing all the annoying template code by hand. However, the actual main advantage of using ACF Blocks is that it makes you architect things in one specific, clean, flexible and modular way. You now have a really good way to separate your templates and functionality. You can re-use and extend ACF field groups and blocks. You always know where to find your code. This approach speeds up the development of smaller projects but it really shines in the context of massive sites where you have lots of different fields and field groups.
todo: add more complex examples.
FAQ
Will using this library have an impact on performance?
No, it's just a really thin layer of abstraction. It doesn't do much, it just allows you to write better code.
I'm using soberwp/controller which already provides me with a Controller. Why should I use this library?
The concept behind soberwp/controller is great, but it doesn't have much use in the context of flexible layouts.