-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Ensure templates filenames without path and extension. #1941
Conversation
I think that might screw up custom page templates, and it can mess with any other hooks into template hierarchy that looks for the template in a subfolder, like certain plugins. #1936 happens because WordPress uses The solution presented here can introduce more bugs so I'm hesitant to accept it. |
Hm, good point! I'm gonna test it with subfolders and some plugins that offer template files and write the feedback here. |
Another approach, without the
function filter_templates($templates)
{
$paths = apply_filters('sage/filter_templates/paths', [
'views',
'resources/views'
]);
return collect($templates)
->map(function ($template) {
return preg_replace('#\.(blade\.?)?(php)?$#', '', ltrim($template));
})
->flatMap(function ($template) use ($paths) {
return collect($paths)
->flatMap(function ($path) use ($template) {
if (strpos($template, '/')) {
$template = preg_replace("#^$path/#", '', $template);
}
return [
"{$path}/{$template}.blade.php",
"{$path}/{$template}.php",
"{$template}.blade.php",
"{$template}.php",
];
});
})
->filter()
->unique()
->all();
} |
@kaisermann Nice 👍 with this soberwp/controller#39 It works as it should. |
Being a little bit picky with the returned paths: The previous code would still return some entries with crazy file paths. function filter_templates($templates)
{
$paths = apply_filters('sage/filter_templates/paths', [
'views',
'resources/views'
]);
$paths_pattern = "#^(" . implode('|', $paths) . ")/#";
return collect($templates)
->map(function ($template) use ($paths_pattern) {
/** Remove .blade.php/.blade/.php from template names */
$template = preg_replace('#\.(blade\.?)?(php)?$#', '', ltrim($template));
/** Remove partial $paths from the beginning of template names */
if (strpos($template, '/')) {
$template = preg_replace($paths_pattern, '', $template);
}
return $template;
})
->flatMap(function ($template) use ($paths) {
return collect($paths)
->flatMap(function ($path) use ($template) {
return [
"{$path}/{$template}.blade.php",
"{$path}/{$template}.php",
"{$template}.blade.php",
"{$template}.php",
];
});
})
->unique()
->filter()
->all();
} Note the |
@kaisermann thanks! could you please push that code to this branch? then we'll get this merged in 👍 |
0f3784d
to
57e20f2
Compare
@retlehs Done 👍 !! |
57e20f2
to
33b981c
Compare
I was reading the solution (#1939) to fix #1936 and came up with an alternative fix by changing the
with