render()
The main method that displays a set of blocks.
Blocks are pulled in the following order
1) A selected layout on a page or a layout preset
2) a post type
3) followed by the blocks on the current page.
Parameters
$args | optional | An array of display options. Specific arguments are explained below. |
default | Defaults to basic_content | When no blocks are set on the current page this is the block that gets loaded as a placeholder. Layout presets don't affect this block being displayed. |
post_id | Defaults to false | The specific post to pull blocks from. Leaving as false will pull the current post id. This is most useful for pages where the block rendering occurs outside of the main WordPress loop such as Archives or Categories. |
type | Defaults to Current post type | Determines which post type is checked for an existing layout. |
Examples
Basic usage
Layout::render();
Setting up a custom post type that doesn't use the block system
Layout::render([
'default' => 'project_body',
]);
flexible()
A helper method for rendering flexible fields. Each layout within the flexible field need to be placed in a folder with their file name matching the layout name. The Field
class is filled and accessible in each block.
Parameters
$blocks | Defaults to [] | An array of blocks. These can be the direct result of Field::get |
$folder | Defaults to blocks | The directory that all the individual files are located. |
Examples
Basic usage
// ['nested_blocks' => [
// ['acf_fc_layout' => 'block1', 'field1' => 'Field 1'],
// ['acf_fc_layout' => 'block2', 'field2' => 'Field 2'],
// ]];
Layout::flexible(Field::get('nested_blocks'), 'nested_blocks');
// Renders:
// /nested_blocks/block1.php
// /nested_blocks/block2.php
id()
Prints out id attribute for a block. The ID is auto generated, but can be explicitly set in the WordPress interface on each block.
Parameters
$prefix | Defaults to block | If the IDs are being auto generated you can specify which string is used as the base for each set of block. |
Examples
Basic usage (assumes Block ID hasn't been set)
Layout::id();
// id="block1"
Using a prefix
Layout::id('sidebar');
// id="sidebar1"
partial()
Displays a view from the "blocks/partials" folder.
Parameters
$view | required | The file name to render, minus the extension. |
$data | optional | An array of data to be used within the view. This information will be set within the Field class. |
Examples
Basic usage
Layout::partial('background');
// Renders:
// /blocks/partials/background.php
Passing additional data
// File: /blocks/partials/support.php
// <?php Field::display('example'); ?>
Layout::partial('support', ['example' => 'Text']);
// Text
partials()
Display multiple views from the "blocks/partials" folder. Each parameter is a view to load.
Parameters
$views | required | Multiple views |
Examples
Basic usage
Layout::partials('background', 'video');
// Renders:
// /blocks/partials/background.php
// /blocks/partials/video.php
getContent()
Return current post's content field formatted with the_content
.
Parameters
None |
Examples
Basic usage
Layout::getContent();
containsBlock()
Check if a particular block has been added to current page. This is extremely useful within functions.php for loading assets.
Parameters
$block | required | Block file name. |
Examples
Basic usage
// ['blocks' => [
// ['acf_fc_layout' => 'map', ...],
// ['acf_fc_layout' => 'content_with_sidebar', ...],
// ]];
Layout::containsBlock('map');
// true
containsBlocks()
Check if the current page contains any of the blocks. Each parameter is a block. This is extremely useful within functions.php for loading assets.
Parameters
$blocks | required | Block file names. |
Examples
Basic usage
// ['blocks' => [
// ['acf_fc_layout' => 'map', ...],
// ['acf_fc_layout' => 'content_with_sidebar', ...],
// ]];
Layout::containsBlocks('map', 'columns');
// true
classes()
Display list of auto generated classes for current block. An additional set of classes can be passed to the method.
Parameters
$classes | required | Class names to prepend to list. |
Examples
Basic usage
// Layout::addClass('class1');
// Layout::addClass('class2');
Layout::classes('class3');
// class3 class1 class2
addClass()
Add a class to the end of the stack for the current block.
Parameters
$class | required | Class name to append to the list. |
Examples
Basic usage
// Layout::addClass('class1');
// Layout::addClass('class2');
Layout::classes('class3');
// class3 class1 class2
addClasses()
Add a set of classes to the beginning of the stack for the current block.
Parameters
$classes | required | Class names to prepend to the list. |
Examples
Basic usage
// Layout::addClasses('class1', 'class2');
Layout::classes('class3');
// class3 class1 class2