在 bolt.cms 中覆盖后端模板
Overwrite backend template in bolt.cms
我正在尝试覆盖位于 vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig
的模板文件(我想替换 "block selection" 下拉菜单)。关于 #1173, #1269, #5588, #3768 and #5102 这在默认情况下是不支持的,所以我必须为此写一个扩展。所以我尝试了这个:
BackendBlockSelectionExtension:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Bolt\Extension\SimpleExtension;
class BackendBlockSelectionExtension extends SimpleExtension
{
public function getServiceProviders()
{
return [
$this,
new BackendBlockSelectionProvider(),
];
}
}
BackendBlockSelectionProvider:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Silex\ServiceProviderInterface;
class BackendBlockSelectionProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$side = $app['config']->getWhichEnd();
if ($side == 'backend') {
$path = __DIR__ . '/App/templates/Backend';
$filesystem = $app['filesystem'];
$filesystem->mountFilesystem('bolt', new Filesystem(new Local($path)));
$app['twig.loader.bolt_filesystem'] = $app->share(
$app->extend(
'twig.loader.bolt_filesystem',
function ($filesystem, $app) {
$path = __DIR__ . 'src/App/templates/Backend/';
$filesystem->prependPath($path, 'bolt');
return $filesystem;
}
)
);
}
}
public function boot(Application $app)
{
}
}
这似乎可以完成工作,但我遇到了一个我完全不明白的错误:The "bolt://app/theme_defaults" directory does not exist.
所以我的最后一个问题是:有没有人有一些示例代码如何在不触及 vendor
文件夹的情况下 overwrite/modify vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig
?
这应该比这简单得多。
在您的扩展程序中 class 覆盖 protected function registerTwigPaths()
函数,如下所示:
protected function registerTwigPaths()
{
if ($this->getEnd() == 'backend') {
return [
'view' => ['position' => 'prepend', 'namespace' => 'bolt']
];
}
return [];
}
private function getEnd()
{
$backendPrefix = $this->container['config']->get('general/branding/path');
$end = $this->container['config']->getWhichEnd();
switch ($end) {
case 'backend':
return 'backend';
case 'async':
// we have async request
// if the request begin with "/admin" (general/branding/path)
// it has been made on backend else somewhere else
$url = '/' . ltrim($_SERVER['REQUEST_URI'], $this->container['paths']['root']);
$adminUrl = '/' . trim($backendPrefix, '/');
if (strpos($url, $adminUrl) === 0) {
return 'backend';
}
default:
return $end;
}
}
现在您可以在您的扩展目录中创建一个视图目录,您可以在其中定义结构中的模板,就像 Bolt 的默认结构一样。我将从复制和覆盖开始。
我正在尝试覆盖位于 vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig
的模板文件(我想替换 "block selection" 下拉菜单)。关于 #1173, #1269, #5588, #3768 and #5102 这在默认情况下是不支持的,所以我必须为此写一个扩展。所以我尝试了这个:
BackendBlockSelectionExtension:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Bolt\Extension\SimpleExtension;
class BackendBlockSelectionExtension extends SimpleExtension
{
public function getServiceProviders()
{
return [
$this,
new BackendBlockSelectionProvider(),
];
}
}
BackendBlockSelectionProvider:
namespace Bundle\Site;
use Bolt\Filesystem\Adapter\Local;
use Bolt\Filesystem\Filesystem;
use Silex\Application;
use Silex\ServiceProviderInterface;
class BackendBlockSelectionProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$side = $app['config']->getWhichEnd();
if ($side == 'backend') {
$path = __DIR__ . '/App/templates/Backend';
$filesystem = $app['filesystem'];
$filesystem->mountFilesystem('bolt', new Filesystem(new Local($path)));
$app['twig.loader.bolt_filesystem'] = $app->share(
$app->extend(
'twig.loader.bolt_filesystem',
function ($filesystem, $app) {
$path = __DIR__ . 'src/App/templates/Backend/';
$filesystem->prependPath($path, 'bolt');
return $filesystem;
}
)
);
}
}
public function boot(Application $app)
{
}
}
这似乎可以完成工作,但我遇到了一个我完全不明白的错误:The "bolt://app/theme_defaults" directory does not exist.
所以我的最后一个问题是:有没有人有一些示例代码如何在不触及 vendor
文件夹的情况下 overwrite/modify vendor/bolt/bolt/app/view/twig/editcontent/fields/_block.twig
?
这应该比这简单得多。
在您的扩展程序中 class 覆盖 protected function registerTwigPaths()
函数,如下所示:
protected function registerTwigPaths()
{
if ($this->getEnd() == 'backend') {
return [
'view' => ['position' => 'prepend', 'namespace' => 'bolt']
];
}
return [];
}
private function getEnd()
{
$backendPrefix = $this->container['config']->get('general/branding/path');
$end = $this->container['config']->getWhichEnd();
switch ($end) {
case 'backend':
return 'backend';
case 'async':
// we have async request
// if the request begin with "/admin" (general/branding/path)
// it has been made on backend else somewhere else
$url = '/' . ltrim($_SERVER['REQUEST_URI'], $this->container['paths']['root']);
$adminUrl = '/' . trim($backendPrefix, '/');
if (strpos($url, $adminUrl) === 0) {
return 'backend';
}
default:
return $end;
}
}
现在您可以在您的扩展目录中创建一个视图目录,您可以在其中定义结构中的模板,就像 Bolt 的默认结构一样。我将从复制和覆盖开始。