Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$template with multiple of same top level block #239

Closed
themorgansmake opened this issue Mar 28, 2024 · 7 comments
Closed

$template with multiple of same top level block #239

themorgansmake opened this issue Mar 28, 2024 · 7 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@themorgansmake
Copy link

Hi there,

Hope all is well.

Is it possible to use the $template value with multiple of the same top level blocks?

I think the issue is that you specify a key of the name of the block and that overrides the previous one (vanilla php stuff etc).

For example, you will only end up with paragraph 3:

`
public $template = [
'core/paragraph' => ['placeholder' => "1"],
'core/paragraph' => ['placeholder' => "2"],
'core/paragraph' => ['placeholder' => "3"],
];

I also tried wrapping them but I don't think the handleTemplate supports this currently:

public $template = [
['core/paragraph' => ['placeholder' => "1"]],
['core/paragraph' => ['placeholder' => "2"]],
['core/paragraph' => ['placeholder' => "3"]],
];`

Not sure if I'm just missing something or if it could be supported at some time?

Thanks in advance!

@Log1x
Copy link
Owner

Log1x commented Mar 28, 2024

I believe the latter (wrapping) should work.

#230 has some more info.

@themorgansmake
Copy link
Author

Thanks for the quick reply @Log1x

I could be wrong but I think that might just be for "InnerBlock" functionality.

When wrapping top level blocks, the $block->template in the blade view spits out something like:

[[0,{"core\/paragraph":{"placeholder":"1"}}],[1,{"core\/paragraph":{"placeholder":"2"}}],[2,{"core\/paragraph":{"placeholder":"3"}}]]

Opposed to when you have different block names:

[["core\/paragraph",{"placeholder":"1"}],["core\/heading",{"placeholder":"2"}]]

@Log1x
Copy link
Owner

Log1x commented Mar 28, 2024

ahhh I see. I will have to look into it when I get a moment but it might be an oversight

@slackday
Copy link

slackday commented Apr 6, 2024

I noticed this too.

This syntax should be supported

public $template = [
  ['core/paragraph' => ['placeholder' => "1"]],
  ['core/paragraph' => ['placeholder' => "2"]],
  ['core/paragraph' => ['placeholder' => "3"]],
];

@slackday
Copy link

slackday commented Apr 9, 2024

Not sure my syntax above is correct after all. I tried both arrays and associative array syntax but nothing worked using the $template property of the block.

I ended up doing the following:

    /**
     * Data to be passed to the block before rendering.
     */
    public function with(): array
    {
        return [
            'allowed_blocks' => esc_attr(wp_json_encode([
                'core/heading',
                'core/paragraph',
            ])),
            'block_template' => esc_attr(wp_json_encode([
                ['core/paragraph', [
                    'placeholder' => '1',
                ]],
                ['core/heading', [
                    'level' => 1,
                    'placeholder' => '2',
                ]],
                ['core/paragraph', [
                    'placeholder' => '3,
                ]],
            ])),
        ];
    }
      <InnerBlocks
        allowedBlocks="{!! $allowed_blocks !!}"
        template="{!! $block_template !!}"
      />

@tombroucke
Copy link
Contributor

Ran into the same issue.
You can just use the $template structure as defined in https://www.advancedcustomfields.com/resources/acf-blocks-using-innerblocks-and-parent-child-relationships/#example-of-passing-a-template-to-innerblocks. And then override the handleTemplate() method:

/**
* The block template.
*
* @var array
*/
public $template = [
    [
        'core/group',
        [
            'layout' => [
                'type' => 'constrained',
            ],
        ],
        [
            [
                'core/paragraph',
                [
                    'align'   => 'center',
                    'content' => 'I\'m a paragraph.',
                ],
                [],
            ],
            [
                'core/separator',
                [],
                [],
            ],
        ],
    ],
];

/**
 * Handle the block template.
 */
public function handleTemplate(array $template = []): Collection
{
    return collect($template);
}

@Log1x Log1x added bug Something isn't working good first issue Good for newcomers labels Jun 4, 2024
@Log1x Log1x closed this as completed in 6f26046 Jul 16, 2024
@Log1x
Copy link
Owner

Log1x commented Jul 16, 2024

This should be better in #267 – check the PR for examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants