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

Block template resolution isn't working on Windows #39024

Open
Mamaduka opened this issue Feb 23, 2022 · 1 comment
Open

Block template resolution isn't working on Windows #39024

Mamaduka opened this issue Feb 23, 2022 · 1 comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. OS Issues Issues or PRs that are related to OS specific problems [Type] Bug An existing feature does not function as intended

Comments

@Mamaduka
Copy link
Member

Originally reported on Twitter by @gmazzap. Thank you, Giuseppe.

They recursively check the theme's "template/" folder to find HTML templates, but they don't normalize found paths.
So templates inside "template/" subfolders are not found on Windows.

https://twitter.com/gmazzap/status/1496037481069916160

@Mamaduka Mamaduka added [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. [Type] Bug An existing feature does not function as intended labels Feb 23, 2022
@gmazzap
Copy link

gmazzap commented Feb 23, 2022

Thank you @Mamaduka

First, sorry for the obvious typo in the tweet (it's templates/)

How to reproduce

In a vanilla WP 5.9 installtion, add the following to the twentytwentytwo functions.php:

add_action(
    'theme_page_templates',
    function ($templates): array {
        return array_merge(['my-folder/my-template.php' => 'My Custom Template'], $templates);
    }
);

Then edit/create a page in WP, and select the "My Custom Template" template.

Now, in the twentytwentytwo templates/ folder, create the my-folder/ folder, and add a my-template.html template.
(Maybe you can copy and rename page.html).

In the template editor you now see the "My Custom Template" template. Change something so you can recognize when it is used.

Go back to the edit screen of the page that has assigned the template and preview it.

If you are on Windows, the template will not be used.

Debugging

The issue happens in the gutenberg_get_block_templates / get_block_templates function.

On Windows, here:

$should_include = $is_not_custom && $fits_slug_query && $fits_area_query;
$should_include is false, because $fits_slug_query is false.

And that because here

! isset( $query['slug__in'] ) || in_array( $template_file['slug'], $query['slug__in'], true );
$query['slug__in'] will contain the array item my-folder/my-template, but $template_file['slug'] is my-folder\my-template.

So the cause is that $template_file['slug'] has Windows directory separator. And that's because _get_block_templates_paths does not normalize the path here:

Notes

More generally, in the whole Gutenberg code I see there a mix of hardcoding / as separator with using DIRECTORY_SEPARATOR. I think a better approach would be to make sure that anywhere there a path, that is normalized.

@t-hamano t-hamano added the OS Issues Issues or PRs that are related to OS specific problems label Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Themes Questions or issues with incorporating or styling blocks in a theme. OS Issues Issues or PRs that are related to OS specific problems [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants